【问题标题】:AJAX CORS webAPI request fails even after the preflight OPTIONS succeeds即使在预检 OPTIONS 成功后,AJAX CORS webAPI 请求也会失败
【发布时间】:2014-11-22 02:58:17
【问题描述】:

我正在尝试对托管在测试服务器中的 Web API 服务进行 CORS AJAX“GET”调用。 webAPI URL = http:xxx:xxx:xxx:xxx/api/v1/jobs

我在 WebAPIConfig.cs 中有以下几行代码

var cors = new EnableCorsAttribute("*","*","*");
config.EnableCors(cors);

AJAX 请求(来自本地)

$.ajax({
      type: "GET",
      datatype: "JSON",
      url: http: xxx: xxx: xxx: xxx / api / v1 / jobs,
      contentType: "application/json";
      charset = utf - 8 ",
            accept: 'application/json',
            beforeSend: BH,            
            success: callback
        }).done(function (data) {
            var str = data.job_id + ': ' + data.job_name;
            $('#responsevalue').text(str);
        }).error(function (jqXHR, textStatus, errorThrown) {
            $('#responsevalue').text(jqXHR.status + "::" + jqXHR.statusText + "::" + jqXHR.responseText );
        });

在 Fiddler 中,我可以看到发送 OPTIONS 的飞行前请求和 200 的响应。

提琴手请求标头:

OPTIONS http:xxx:xxx:xxx:xxx/api/v1/jobs HTTP/1.1
Host: 50.17.211.226
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://localhost:55346
Access-Control-Request-Method: GET
Access-Control-Request-Headers: requestdateutc,requestverificationtoken
Connection: keep-alive

提琴手响应标头

HTTP/1.1 200 OK
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Accept
Access-Control-Max-Age: 1728000
X-Powered-By: ASP.NET
Date: Thu, 20 Nov 2014 14:21:50 GMT
Content-Length: 0

在 Firebug 中,我可以看到以下详细信息:

Firebug 请求标头:

OPTIONS http:xxx.xxx.xxx.xxx/api/v1/jobs HTTP/1.1
Host: xxx.xxx.xxx.xxx
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http:localhost:55346
Access-Control-Request-Method: GET
Access-Control-Request-Headers: requestdateutc,requestverificationtoken
Connection: keep-alive

Firebug 响应标头:

HTTP/1.1 200 OK
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: *, *
Access-Control-Allow-Methods: GET, POST, PUT, DELETE
Access-Control-Allow-Headers: Content-Type, Accept, Content-Type
Access-Control-Max-Age: 1728000
X-Powered-By: ASP.NET
Date: Thu, 20 Nov 2014 18:08:36 GMT
Content-Length: 0

我在这里和其他地方阅读了很多文档。它看起来很简单,适用于所有人(除了我)。

最后说明:我使用驻留在测试服务器内部的 html 页面测试了 API,它运行良好。这意味着服务和网页都位于同一个域中。 附加信息:浏览器:Firefox、ASP.NET 4.5、Web API 2.2、VS2013 Express

提前致谢,我们将不胜感激。

【问题讨论】:

    标签: asp.net-web-api


    【解决方案1】:

    在您的回复中,它说允许的标头是Content-Type, Accept, Content-Type,但您要求的是requestdateutc,requestverificationtoken。尝试明确允许这些标头。

    【讨论】:

      猜你喜欢
      • 2014-07-26
      • 1970-01-01
      • 2021-08-25
      • 2018-03-20
      • 2017-04-19
      • 2015-08-04
      • 2014-02-14
      • 2018-01-29
      • 2015-12-24
      相关资源
      最近更新 更多