AngularJS:XHR向PUT / POST请求返回net :: ERR_METHOD_NOT_SUPPORTED错误

时间:2014-09-13 17:29:22

标签: angularjs xmlhttprequest cors

我在处理从服务器到PUT请求的响应时遇到问题(与POST相同)。从以下CORS飞行前请求开始。

请求:

OPTIONS /open/Patient/1 HTTP/1.1
Host: remoteserver.com
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: PUT
Origin: http://myclient
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Access-Control-Request-Headers: accept, content-type
Accept: */*
DNT: 1
Referer: http://myclient/patient/2JT
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

响应:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 0
Date: Sat, 13 Sep 2014 16:58:28 GMT
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Location, Location
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Headers: accept, content-type
Access-Control-Request-Method: GET, POST, PUT, DELETE
Server: Name of remote server

然后是实际请求:

PUT /open/Patient/1 HTTP/1.1
Host: remoteserver.com
Connection: keep-alive
Content-Length: 775
Pragma: no-cache
Cache-Control: no-cache
Accept: application/json+fhir, application/json, text/plain, */*
Origin: http://myclient
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36
Content-Type: application/json+fhir
DNT: 1
Referer: http://myclient/patient/2JT
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

AngularJS不处理响应,而是报告以下错误:

净:: ERR_METHOD_NOT_SUPPORTED

响应正文为空,状态为“0”。事实是,如果我在Chrome调试器中设置了一个断点,在单步执行代码后,我会得到一个成功的响应。这是我在回复中得到的结果:

HTTP/1.1 200 OK
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json+fhir; charset=UTF-8
Content-Length: 154
Date: Sat, 13 Sep 2014 17:13:35 GMT
Pragma: no-cache
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Location, Location
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Content-Location: http://remoteserver/open/Patient/1/_history/6
Server: Health Intersections FHIR Server

以下是使用$ http:

发送请求的代码正文
function updateResource(resourceUrl, resource) {
    var deferred = $q.defer();
    $http.put(resourceUrl, resource)
        .success(function (data, status, headers, config) {
            var results = {};
            results.data = data;
            results.headers = headers();
            results.status = status;
            results.config = config;
            deferred.resolve(results);
        })
        .error(function (data, status) {
            var error = { "status": status, "outcome": data };
            deferred.reject(error);
        });
    return deferred.promise;
}

1 个答案:

答案 0 :(得分:0)

我已经与服务器的开发人员一起解决了这个问题。事实证明服务器处理连接的方式存在问题。解决。