JQuery get()或getJSON仅适用于本地文件 [英] JQuery get() or getJSON only work for local files
问题描述
我打算从我的服务器加载一个test.json文件。试图加载该文件的index.html位于另一台服务器上(实际上它是一个本地文件)。
I'm tring to load a test.json file from my server. The index.html which trys to load that file is on another server(infact its a local file).
$url = 'test.json';
$.getJSON($url, function(data) {
for (var i = 0, len = data.length; i < len; i++) {
//some code
}
});
完美无缺。
但是当我设置 $ url ='http://simonappelt.de/test.json'
它什么也不做。
我也尝试过使用get()函数从我的服务器获取文件,但它们并没有加载任何东西。
works perfect.
but when i set $url = 'http://simonappelt.de/test.json'
it doesn't do anything.
I also tried the get() function to get files from my server but they seam to load nothing.
感谢Simon
推荐答案
通常情况下,您只能对同一个AJAX请求(这就是 $。getJSON
是)域和协议作为您的脚本。如果您希望提出跨域请求,则必须使用 JSON-P支持$ .getJSON ,并将数据包装在由查询字符串参数?callback
确定的回调调用服务器端。
Typically you can only make an AJAX request (which is what $.getJSON
is) on the same domain and protocol as your script. If you wish to make a cross-domain request, you will must use the JSON-P support of $.getJSON, and wrap the data in a callback invocation server side, determined by the query string parameter, ?callback
.
这篇关于JQuery get()或getJSON仅适用于本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!