javascript - 调试 MobileServiceClient 请求

标签 javascript azure asp.net-web-api xmlhttprequest asp.net-web-api2

我正在开发 Azure MobileService/CordovaApp 设置。服务器在本地运行并具有以下设置来启用 CORS:

<add name="Access-Control-Allow-Origin" value="*" />

可以使用诸如

之类的地址通过浏览器调用 API
http://localhost:59477/api/item/explore/A/B

尝试描述此调用的客户端脚本如下:

var client = new WindowsAzure.MobileServiceClient('http://localhost:59477/', 'http://localhost:59477/', '');
GetItems.addEventListener("click",
function ()
{
    client.invokeApi("item/explore/A/B",
        {
            method: "get"
        })
        .done(
        function (results)
        {
            alert(results.result.count);
        },
        function (error)
        {
            var xhr = error.request;
            alert('Error - status code: ' + xhr.status + '; body: ' + xhr.responseText);
            alert(error.message);
        }
    );
}
);

我得到的是状态 405 - 方法不允许,我不明白有两个原因:

  • 调用的服务操作由 [HttpGet] 属性修饰
  • 响应 header 似乎表明 GET 没问题:
    HTTP/1.1 405 Method Not Allowed
    Allow: GET
    Content-Length: 76
    Content-Type: application/json; charset=utf-8
    Server: Microsoft-IIS/8.0
    X-SourceFiles: =?UTF-8?B?RjpcQ29kZVxGb290UHJpbnRzXGZvb3RwcmludHMuU2VydmVyXEZvb3RwcmludHNTZXJ2aWNlXGFwaVxmb290cHJpbnRcZXhwbG9yZVw1MS4yNzcwMjJcNy4wNDA3ODM=?=
    X-Powered-By: ASP.NET
    Access-Control-Allow-Origin: *
    Date: Sat, 15 Aug 2015 18:08:30 GMT

Any idea what I can do to get this running?


Edit The Raw request shows that, as already expected by Jeremy Foster, the client sends a request of the type OPTIONS:

OPTIONS http://localhost:59477/api/fp/get?id=1 HTTP/1.1
Host: localhost:59477
Connection: keep-alive
Access-Control-Request-Method: GET
Origin: http://localhost:4400
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.155 Safari/537.36
Access-Control-Request-Headers: accept, x-zumo-features, x-zumo-installation-id, x-zumo-version
Accept: */*
Referer: http://localhost:4400/index.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

I managed to support this request applying the [HttpOptions] attribute to my action method. Now I get a more or less valid response:

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
Server: Microsoft-IIS/8.0
X-SourceFiles: =?UTF-8?B?RjpcQ29kZVxGb290UHJpbnRzXGZvb3RwcmludHMuU2VydmVyXEZvb3RwcmludHNTZXJ2aWNlXGFwaVxmcFxnZXRcMQ==?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept
Date: Sun, 23 Aug 2015 19:34:21 GMT
Content-Length: 234

         ...

At least that's what fiddler tells me. The AzureMobileServiceClient throws yet another issue, which roughly translates as:

Cross-Origin request blocked, missing token 'x-zumo-installation-id' in CORS header row 'Access-Control-Allow-Headers' on CORS-Preflight-Channel

最佳答案

打开Fiddler并尝试从浏览器进行 API 调用,然后再次使用移动服务客户端并比较您的原始 HTTP 请求以查看有何不同。

关于javascript - 调试 MobileServiceClient 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32027866/

相关文章:

Azure:存储交易成本

c# - 你能从 web api Controller 同时调用 GET 和 POST 吗?

asp.net-mvc - 传回多个服务uri

javascript - 如何用 jquery 返回 php json 数组对象?

javascript - Sinon stub 无法与 module.exports = { f1, f2} 一起使用

javascript - 无法访问 jQuery DOM 就绪范围以进行调试

azure - 将 Azure Application Insights 数据发送到事件中心

azure - Web 应用程序使用 OAuth 2.0 授权流程对连接到 Azure AD 的 ADFS 进行身份验证

javascript - webpack 使用什么方法在开发工具中显示 'webpack://' 文件夹中的源?

asp.net-web-api - 如何将带空格的参数发送到 .net web api