【问题标题】:XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present. (Apache/jQuery/AJAX)XMLHttpRequest 无法加载。不存在“Access-Control-Allow-Origin”标头。 (阿帕奇/jQuery/AJAX)
【发布时间】:2014-09-23 20:10:01
【问题描述】:

这似乎是一个很常见的问题,我已经阅读了几十个关于这个主题的主题。但是,到目前为止,没有任何回应对我有用。基本上我有一个带有 jQ​​uery/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


【解决方案1】:

在我的服务器上,我必须在我的虚拟主机目录指令中明确设置 url:

Header set Access-Control-Allow-Origin "http://mysite.mydomain.com"

在将接收请求的网站上。

【讨论】:

    猜你喜欢
    • 2014-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    相关资源
    最近更新 更多