ajax jQuery:无法从其他域获取JSON/xml [英] ajax jQuery: can't get JSON/xml from other domain

查看:60
本文介绍了ajax jQuery:无法从其他域获取JSON/xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$(document).ready(function() {
    $.getJSON('http://192.168.99.68/test.json?callback=?', function(json) {    
        alert("abc");
    });
});

它不起作用. 在Linux VM上有一个192.168.99.68的JSON文件. 当我在浏览器上运行http://192.168.99.68/test.json?callback=?时,它将返回真实的JSON.

It doesn't work. There is a JSON file on linux VM with 192.168.99.68. When I run http://192.168.99.68/test.json?callback=? on browser, it returns true JSON.

推荐答案

如果此json文件中包含回调函数,则意味着它是动态创建的,对吗? 因此,如果它在php中,并且您是json文件的所有者,则可以使用以下标头:

If there is a callback function in this json file, it means that it's created dynamically, right? So, if it is in php and you are the owner of the json file, you can use this header:

header('Access-Control-Allow-Origin: *');

您可以将*替换为将访问此json文件的域. *表示JavaScript允许所有域访问.

You can replace * with the domain, that will access to this json file. * means, that all domains are allowed to access by JavaScript.

*或*

如果您不是所有者,或者您不想编辑json文件的标题,则可以使用该json文件似乎支持的回调函数:

If you aren't the owner or you don't want to edit the headers of the json file, you can use the callback function, that the json file seems to support:

<script type="text/javascript">
function getJSON(json) {
    alert("Got JSON!");
    // do something with json
}
</script>
<script type="text/javascript" src="http://192.168.99.68/test.json?callback=getJSON" async="true"></script>

这篇关于ajax jQuery:无法从其他域获取JSON/xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆