角JS get请求与包头中表示错误CORS
问题描述:
以我角控制器代码我的HTTP GET方法不与头工作,但同样的请求在邮递员工作角JS get请求与包头中表示错误CORS
$http({
method: 'GET',
url: 'http://localhost:8080/api/profiles',
headers: {
'Authorization': 'Bearer 78954642-sf25-4sd6-sdf2-99582369d5af',
'Content-Type':'text/plain'
}
}).then(function(success) {
console.log(success);
}, function(error) {
});
显示以下错误
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://quotebuilder' is therefore not allowed access. The response had HTTP status code 401.
我的请求头
OPTIONS /api/profiles HTTP/1.1
Host: localhost:8080
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Access-Control-Request-Method: GET
Origin: http://quotebuilder
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Access-Control-Request-Headers: authorization
Accept: */*
Referer: http://quotebuilder/login
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-US,en;q=0.8
什这里错了吗?请帮我解决问题
答
你应该在你的服务器端禁用CORS来完成这项工作。当资源请求来自不同域的资源时,资源会创建一个跨源HTTP请求,或者资源本身的服务器端看到的资源为here。如果您使用的是Spring或其他服务器端Web框架,则需要从那里禁用它。
您需要从chrome安装CORS插件,或者应该从API启用CORS。 – Rajesh
我认为你可以传递响应头文件,它应该可以工作。当我在我的'response'头文件中添加'.headers(Access-Control-Allow-Methods“,”GET,POST,DELETE,PUT,OPTIONS“)时,它对我有效,如果这样做不起作用,那么编辑你的网页。 xml文件(如果使用的是tomcat) –