使用CORS的Angular HTTP Post在幕后工作,但不会“成功”

时间:2013-08-07 10:33:40

标签: asp.net-mvc http angularjs cors

我正在开发AngularJS项目,我需要执行跨域(CORS)POST 。 我花了很长时间才开始工作,好吧,它现在有点工作:

  • 正常发送(OPTIONS)请求和我的服务器 也正确地回应它。
  • 发送实际的POST请求 正确地,我的服务器收到它,并返回201 (创造)应该的回应。

那么问题是什么?

然后,Angular $ http对象不会转到“success”回调,而是转到“error”回调而是...状态代码为0且没有特定错误。 所以从我的页面来看,我无法知道我的请求是否真的有效。我知道它确实有效的唯一方法是控制(调试)服务器并使用Fiddler。

以前有人遇到过这个问题吗?拥有一个可行的解决方案但却无法说明它确实有效,这有点令人沮丧:)

这是我的$ http请求:

this.simulate = function (url, content) {
var deferred = $q.defer();

var data = { "Data": content, "Timestamp": new Date() };

$http.defaults.useXDomain = true;
$http.post(url, data)
    .then(function(response) {
        deferred.resolve({
            isSuccess: true,
            httpCode: response.status,
            errorMessage: "",
            url: url,
            data:data
        });
    },function(response) { // This is this error callback that is being called, despite the fact that my request is working fine...
        deferred.resolve({
            isSuccess: false,
            httpCode: response.status,
            errorMessage: response.data.Message + " " + response.data.ExceptionMessage,
            url: url,
            data: data
        });
    });

return deferred.promise;
};

在小提琴手中,这就是我得到的:

飞行前请求:

OPTIONS http://myServer:82/xclient/event/volupdate HTTP/1.1
Host: myServer:82
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://myClient:1855
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Access-Control-Request-Headers: accept, origin, x-requested-with, authorization, ssotoken, content-type
Accept: */*
DNT: 1
Referer: http://myClient:1855/XClient/testharness/eventing
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6,fr-FR;q=0.4

这给了我飞行前反应:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/7.5
Access-Control-Allow-Origin: http://myClient:1855
Access-Control-Allow-Methods: POST
Access-Control-Allow-Headers: accept, origin, x-requested-with, authorization, ssotoken, content-type
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 07 Aug 2013 10:13:05 GMT
Content-Length: 0

然后是实际的POST请求:

POST http://myServer:82/xclient/event/volupdate HTTP/1.1
Host: myServer:82
Connection: keep-alive
Content-Length: 56
Origin: http://myClient:1855
Authorization: SSOB2L1ax<BLAHBLAHBLAH>EP49w=|0|jaussan|System X|20130825131712|1748001|
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36
Content-Type: application/json;charset=UTF-8
Accept: application/json, text/plain, */*
X-Requested-With: XMLHttpRequest
DNT: 1
Referer: http://myClient:1855/XClient/testharness/eventing
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6,fr-FR;q=0.4

{"Data":"SomeData","Timestamp":"2013-08-07T10:13:06.533Z"}

然后是一个正常的 201创建的对POST的响应:

HTTP/1.1 201 Created
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 07 Aug 2013 10:13:05 GMT
Content-Length: 0

正如你所看到的一切看似正常,但是$ http似乎认为它不是。

以下是Firebug的一些截图:

$ http进入错误的回调: $http goes to the wrong callback

这是我从$ http得到的回复...不是我的201!: And this is the response I'm getting from $http... not really my 201!

1 个答案:

答案 0 :(得分:4)

好像您的服务器正在正确处理OPTIONS(预检)请求,而不是实际的POST请求。您的服务器至少需要在对POST请求的响应中包含Access-Control-Allow-Origin标头。在您的情况下,那将是:Access-Control-Allow-Origin: http://myClient:1855

为了将来参考,有一篇关于CORS的优秀文章,每个人谁必须支持跨源环境应绝对标记:MDN on HTTP access control