javascript – CORS错误:请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – CORS错误:请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1572字,纯文字阅读大概需要3分钟。
内容图文
![javascript – CORS错误:请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权](/upload/InfoBanner/zyjiaocheng/710/7fd19e684bc44a78ae5999fdca062650.jpg)
我试图将请求从一个localhost端口发送到另一个.我在后端的节点和节点上使用angularjs.
由于它是CORS请求,在node.js中,我正在使用
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, Authorization');
在angular.js服务文件中,我正在使用
return {
getValues: $resource(endpoint + '/admin/getvalues', null, {
'get': {
method: 'GET',
headers:{'Authorization':'Bearer'+' '+ $localStorage.token}
}
}),
}
我收到以下错误
请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权.
请帮忙!
解决方法:
您还必须在允许的标头中添加选项.浏览器在发送原始请求之前发送预检请求.见下文
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
来源https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/OPTIONS
In CORS, a preflight request with the OPTIONS method is sent, so that the server can respond whether it is acceptable to send the request with these parameters. The
Access-Control-Request-Method
header notifies the server as part of a preflight request that when the actual request is sent, it will be sent with a POST request method. TheAccess-Control-Request-Headers
header notifies the server that when the actual request is sent, it will be sent with aX-PINGOTHER
andContent-Type
custom headers. The server now has an opportunity to determine whether it wishes to accept a request under these circumstances.
EDITED
你可以使用npmjs.com/package/cors npm package避免这种手动配置.我也使用过这种方法,它清晰简单.
内容总结
以上是互联网集市为您收集整理的javascript – CORS错误:请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权全部内容,希望文章能够帮你解决javascript – CORS错误:请求标头字段预检响应中的Access-Control-Allow-Headers不允许授权所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。