0

here's my query:

 var query = breeze.EntityQuery
                .from("Mandates").skip(offset).take(pageSize).inlineCount(true);

        return manager.executeQuery(query);

In the response headers, I can see the count is returned by the server:

X-InlineCount:63

However, in the succeed handler when I do:

 var results = data.results;
 var recordCount = data.inlineCount;

results contains the correct records. However data.inlineCount is null.

How come ?

EDIT

getAllResponseHeaders does not return all headers ! I've just noticed most of them are missing, including X-InlineCount.

I believe this is because I'm doing a cross-domain http request (website is in localhost but my webapi (web service) is on a server on the domain). How can this be fixed ?

4

2 回答 2

3

我们已经确认了问题。我们计划通过将inlineCount标头移出并进入结果有效负载来修复它,它可能首先应该放在首位。没有确定的日期,但很快。

更新:2013 年 4 月 18 日。我们已经在内部实现了这个修复并且它通过了我们的测试。我们将在下一个 NuGet(1.3.0 之后的版本)中进行部署。发生这种情况时,我会再次更新此答案。

我们通过调整这个 jsFiddle重现了这个问题,任何人都可以这样做。

小提琴向公开 Todos 的 Breeze 服务器发出请求。自然,Breeze 服务器与 jsFiddle 位于不同的域中,因此需要 CORS 来执行查询和保存操作。

我将getAllTodos方法更改为以下内容:

函数 getAllTodos() {
    var 查询 = 微风.EntityQuery.from("Todos")

    // 为 Cross-Origin inlineCount 标头测试添加
    .orderBy('描述')
    .skip(1).take(3)
    .inlineCount(true);

    log("获取待办事项");
    返回 manager.executeQuery(查询)
    .then(querySucceeded).fail(失败);

    函数查询成功(数据){
        var count = data.results.length;
        log("检索到" + count);

        // 为 Cross-Origin inlineCount 标头测试添加
        log("内联计数:"+ data.inlineCount);
        log("标题:" + data.XHR.getAllResponseHeaders());

        如果(!计数){
            log("没有待办事项"); 返回;
        }
        viewModel.items(data.results);
    }
}

@jvrdelafuente - 我很惊讶您推荐的 Web.config 更改确实有效。我原以为对 Web.config 的这种更改只会影响服务器的功能,而不会影响浏览器的功能。

当我检查网络流量时,我可以看到服务器正在发送“X-InlineCount”标头,无论是否有您建议的更改。浏览器(至少是 IE10 和 Chrome)似乎隐藏了 jQuery.AJAX 的标头,它负责典型微风应用程序中的 AJAX 通信。

当我将以下内容添加到查询成功回调时,我看到了这种效果:

console.log("标题:" + data.XHR.getAllResponseHeaders());

日志显示服务器实际返回的标头很少。

也许当你添加

<add name="Access-Control-Expose-Headers" value="X-InlineCount" />

对于 Web.config,服务器可以告诉浏览器可以将“X-InlineCount”标头公开给 JavaScript 客户端。

我承认我没有调查这种可能性。也许在短期内这是一个可行的解决方法。

但就我们而言,这只是一个创可贴。我们不希望开发人员担心这种事情;CORS 确实令人头疼。因此,我们将inlineCount在有效负载中而不是在标头中进行通信……并一起消除这个问题。

于 2013-04-18T18:20:45.013 回答
1

我有同样的问题。您必须配置您的服务器以公开 X-InlineCount 标头。如果您使用的是项目 ASP .NET Web API,则必须在 web.config 中添加下一行:

  <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
        <add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
        <add name="Access-Control-Expose-Headers" value="X-InlineCount" />
      </customHeaders>
    </httpProtocol>

另外,我遇​​到了firefox的问题,但是在firefox的最新版本中,问题得到了解决。

(你不需要使用所有的行,这只是一个例子);)

- - -编辑 - - -

我忘记了,因为那是很久以前的事了。当您进行 CORS 调用时,浏览器首先发送一个 Option 调用以检查是否可以进行 CORS 调用以及允许执行哪些操作。所以你必须在你的 WEB API 项目中实现一个消息处理程序来管理这个 OPTION 调用。接下来是实现:

public class OptionsHttpMessageHandler : DelegatingHandler
    {
        protected override Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (request.Method == HttpMethod.Options)
            {
                var apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();


                var controllerRequested = request.GetRouteData().Values["controller"] as string;
                var supportedMethods =  apiExplorer.ApiDescriptions
                    .Where(d =>
                    {

                        var controller = d.ActionDescriptor.ControllerDescriptor.ControllerName;
                        return string.Equals(
                            controller, controllerRequested, StringComparison.OrdinalIgnoreCase);
                    })
                    .Select(d => d.HttpMethod.Method)
                    .Distinct();

                if (!supportedMethods.Any())
                    return Task.Factory.StartNew(
                        () => request.CreateResponse(HttpStatusCode.NotFound));

                return Task.Factory.StartNew(() =>
                {
                    var resp = new HttpResponseMessage(HttpStatusCode.OK);
                    resp.Headers.Add("Access-Control-Allow-Origin", "*");
                    resp.Headers.Add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Security,Token-Access");
                    resp.Headers.Add("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
                    resp.Headers.Add("Access-Control-Expose-Headers", "X-InlineCount");

                    return resp;
                });
            }

            return base.SendAsync(request, cancellationToken);
        }
    }

如果你把它放在你的 web.config 中,你可以省略下一节。

resp.Headers.Add("Access-Control-Allow-Origin", "*");
resp.Headers.Add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type, Accept, Security,Token-Access");
resp.Headers.Add("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
resp.Headers.Add("Access-Control-Expose-Headers", "X-InlineCount");

我希望这有助于解决您的问题。

PD:对不起,我的英语不是我的母语。

于 2013-04-18T16:43:22.570 回答