【发布时间】:2014-09-23 20:10:01
【问题描述】:
这似乎是一个很常见的问题,我已经阅读了几十个关于这个主题的主题。但是,到目前为止,没有任何回应对我有用。基本上我有一个带有 jQuery/AJAX 脚本的页面,它可以拉出一对
来自外部(跨域)页面的元素。
我已尝试将其放入我的 .htaccess 文件中:
Header set Access-Control-Allow-Origin "*"
还有:
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
但没有运气。我的 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.');
}
});
当我从桌面运行页面时一切正常,但从实际托管的服务器上,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.
有什么想法吗?
【问题讨论】:
-
.htaccess 文件位于 external_url 服务器右侧。
-
遇到同样的问题,Safari 和 Chrome 都会产生相同的结果。有什么解决办法吗?
标签: jquery ajax apache .htaccess cross-domain