且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Ajax 向 http 请求添加 CORS 标头

更新时间:2022-04-23 06:10:02

你不能这样授权自己.这是一个响应标头;规范中的详细信息.您要向其发送请求的服务器必须将该标头发回,以便让浏览器知道允许您的页面向该服务器发送 ajax 请求是可以的.如果您尝试请求的服务器不允许您的源,则您无法在客户端代码中执行任何操作.

You can't authorize yourself like that. It's a response header; details in the specification. The server you're sending the request to has to send that header back to let the browser know it's okay to allow your page to send an ajax request to that server. There's nothing you can do in your client-side code if the server you're trying to request from doesn't allow your origin.