jQuery 如何在服务器上启用跨域请求?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6871021/
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
How to enable cross-domain request on the server?
提问by Mahendra Liya
I have a json file hosted on my server. When I try to make an Ajax "GET" request to the json file, it fails.
我的服务器上托管了一个 json 文件。当我尝试向 json 文件发出 Ajax“GET”请求时,它失败了。
See the console in Safari, it says "Failed to load resource".
查看 Safari 中的控制台,它显示“无法加载资源”。
Firebug shows "200 OK", but the response doesn't show up. Even Firebug does not show the JSON tab.
Firebug 显示“200 OK”,但没有显示响应。即使 Firebug 也不显示 JSON 选项卡。
I believe this is because Cross Domain Requests are not allowed using AJAX.
我相信这是因为使用 AJAX 不允许跨域请求。
I would like to know how can I overcome this? Also, if I want to enable cross-domain requests on my server, I believe a crossdomain.xml
file or something needs to be created. I am not sure, but this is what I know. I searched on Google, but could not find any relevant links.
我想知道我怎样才能克服这个问题?另外,如果我想在我的服务器上启用跨域请求,我相信crossdomain.xml
需要创建一个文件或其他东西。我不确定,但这是我所知道的。我在谷歌上搜索,但找不到任何相关链接。
Any help in this is highly appreciated.
非常感谢这方面的任何帮助。
Thanks.
谢谢。
UPDATE:I am not using any server-side scripting language (PHP, ASP.NET, etc). I am using Plain HTML and JavaScript / jQuery.
更新:我没有使用任何服务器端脚本语言(PHP、ASP.NET 等)。我正在使用纯 HTML 和 JavaScript/jQuery。
UPDATE-2:
更新-2:
I used the following code to make cross-domain requests:
我使用以下代码进行跨域请求:
<script src="jquery-1.6.2.js"></script>
<script>
$(document).ready(function () {
$.ajax({
dataType: 'jsonp',
data: '',
jsonp: 'jsonp_callback',
url: 'http://myhosting.net/myjsonfile.json',
success: function (jsonData) {
alert("success")
alert(jsonData);
},
error: function(errorObj) {
alert(errorObj.statusText);
},
});
});
When i see in Firebug's "Net" tab, I see a JSON tab, and I am able to see the json response. However, the "success" callback handler doesn't get called, but the "error" callback handler gets invoked and I get the alert saying parseerror
.
当我在 Firebug 的“Net”选项卡中看到时,我会看到一个 JSON 选项卡,并且能够看到 json 响应。但是,“成功”回调处理程序没有被调用,但“错误”回调处理程序被调用,我收到警报说parseerror
.
Any idea what could be wrong?
知道有什么问题吗?
回答by genesis
Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com
on target server
在目标服务器上
in php:
在 php 中:
header("Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com");
in case you don't want to use server-scripting language: put this in (linux) console
如果你不想使用服务器脚本语言:把它放在(linux)控制台中
a2enmod headers
and to your .htaccess file add - - - - - - -
并在您的 .htaccess 文件中添加 - - - - - - -
Header set Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com
回答by boksiora
Put this in your .htaccessand plain ajax works
把它放在你的.htaccess和普通的 ajax 作品中
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
回答by boksiora
the solution given by genesis worked for me, however I had to omit the trailing slash on the url. ie:
Genesis 给出的解决方案对我有用,但是我不得不省略 url 上的尾部斜杠。IE:
header("Access-Control-Allow-Origin: http://yourdomain-you-are-connecting-from.com");