jQuery ajax 发布请求是请求的资源上不存在“Access-Control-Allow-Origin”标头。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20211093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:02:50  来源:igfitidea点击:

ajax post request is No 'Access-Control-Allow-Origin' header is present on the requested resource.'

jqueryajaxcross-domaincross-domain-policy

提问by harishkumar329

I'm just making an ajax post request and I'm getting an error like:

我只是在发出一个 ajax 发布请求,但出现了如下错误:

XMLHttpRequest cannot load https://xxx.com?redirect_uri=http://www.example.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

XMLHttpRequest 无法加载 https://xxx.com?redirect_uri=http://www.example.com。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问源“http://example.com”。

And here I can see there is nothing different from my response domain and request domain except "www", so does "www" also cause this issue?

在这里我可以看到除了“www”之外,我的响应域和请求域没有什么不同,那么“www”是否也会导致这个问题?

回答by Rory McCrossan

The problem is because you are making a cross-domain AJAX request, which prevented by browser security - see the Same Origin Policy.

问题是因为您正在发出跨域 AJAX 请求,而浏览器安全性阻止了该请求 - 请参阅Same Origin Policy

The request is expecting you to be making a request to a CORS enabled domain, hence why it is complaining about the non-existant header.

该请求期望您向启用 CORS 的域发出请求,因此它抱怨不存在的标头。

You either need to change your request to jsonptype, or use a server-side proxy to get the data.

您要么需要更改请求jsonp类型,要么使用服务器端代理来获取数据。

回答by Sydney Collins

When requesting data from external website it is always best to not do it in ajax , but if you have to do it an ajax have a internal page which makes a call with server side code and call that page with ajax.

当从外部网站请求数据时,最好不要在 ajax 中进行,但如果你必须这样做,ajax 有一个内部页面,它使用服务器端代码进行调用并使用 ajax 调用该页面。