[英]XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present. (Apache/jQuery/AJAX)
This seems to be a pretty common question on here, and I've through a couple dozen threads on the topic. 这似乎是一个很普遍的问题,我已经通过了数十个关于该主题的话题。 However, none of the responses have worked for me so far. 但是,到目前为止,没有任何回应对我有用。 Basically I have a page with a jQuery/AJAX script that pulls a couple 基本上我有一个带有jQuery / AJAX脚本的页面
elements from an external (cross-domain) page. 外部(跨域)页面中的元素。
I've tried putting this in my .htaccess file: 我尝试将其放在我的.htaccess文件中:
Header set Access-Control-Allow-Origin "*"
as well as: 以及:
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
but no luck. 但没有运气。 My AJAX call is: 我的AJAX电话是:
$.ajax({
type: 'GET',
url: 'url', //I have the actual URL here of course
cache: false,
contentType: 'text/plain',
dataType: 'html',
crossDomain: true,
success: function( data ) {
//doing stuff with data in here
}, error: function(jqXHR, textStatus, errorThrown) {
alert('critical error. everything is exploding. abort mission.');
}
});
Everything works perfectly when I run the page from my desktop, but from the actual hosted server, the console error in Chrome reads: 当我从桌面上运行页面时,一切工作正常,但是从实际的托管服务器上,Chrome中的控制台错误显示为:
"XMLHttpRequest cannot load http://external_url.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://my_url.com' is therefore not allowed access.
Any ideas? 有任何想法吗?
On my server I had to explicitely set the url in my vhost directory directive: 在我的服务器上,我必须在vhost目录指令中显式设置url:
Header set Access-Control-Allow-Origin "http://mysite.mydomain.com"
on the site that will recieve the request. 在将收到请求的网站上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.