简体   繁体   English

仅在“放置”时才会发生cors问题?

[英]Cors issue occurs only when “put”?

I'm building an Angular app which use ng-resource. 我正在构建一个使用ng-resource的Angular应用程序。 The service API is built in Asp.Net Core web api and the CORS is already enabled. 服务API内置于Asp.Net Core Web api中,并且已启用CORS。

The service.js code service.js代码

.factory('D2Service', ['$resource', function ($resource) {
    return $resource('http://localHost:5001/api/D2/:id',
        { id: '@id' },
        {
            update: { method: 'PUT' }
        });
}])

However, the call D2Service.update(model.d); 但是,调用D2Service.update(model.d); in controller got the following error. 控制器中出现以下错误。

XMLHttpRequest cannot load http://localhost:5001/api/D2 . XMLHttpRequest无法加载http:// localhost:5001 / api / D2 Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 对预检请求的响应未通过访问控制检查:请求的资源上不存在“ Access-Control-Allow-Origin”标头。 Origin ' http://localhost:8082 ' is therefore not allowed access. 因此,不允许访问源' http:// localhost:8082 '。

But D2Service.get({ id: model.id }).... runs without any error. 但是D2Service.get({ id: model.id })....运行没有任何错误。 Why the 'PUT' has the CORS issue while the 'GET' is fine? 为什么在“ GET”正常的情况下,“ PUT”会出现CORS问题?

The following are the request/response when monitoring using fiddler. 以下是使用提琴手进行监视时的请求/响应。

Request: 请求:

OPTIONS http://localhost:5001/api/D2 HTTP/1.1
Host: localhost:5001
Connection: keep-alive
Access-Control-Request-Method: PUT
Origin: http://localhost:8082
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Access-Control-Request-Headers: content-type
Accept: */*
Referer: http://localhost:8082/
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8

Response: 响应:

HTTP/1.1 204 No Content
Date: Sun, 27 Nov 2016 23:32:31 GMT
Server: Kestrel

For preflight request to work you should have following header in your response : 为了使印前检查工作正常,您应在response包含以下标头:

Access-Control-Allow-Origin: YOUR_DOMAIN
Access-Control-Request-Method: PUT
Access-Control-Request-Headers: YOUR_CUSTOM_HEADERS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM