jQuery的的getJSON不工作跨站点站点、工作、jQuery、getJSON

由网友(壞孩孓)分享简介:我有一块的JavaScript,抓住JSON数据。在本地执行的一切似乎很好地工作。然而,当我试着从不同的站点访问它,这是行不通的。 I have a piece of javascript that grabs JSON data. When executed locally everything seems to...

我有一块的JavaScript,抓住JSON数据。在本地执行的一切似乎很好地工作。然而,当我试着从不同的站点访问它,这是行不通的。

I have a piece of javascript that grabs JSON data. When executed locally everything seems to work fine. However, when I try accessing it from a different site, it doesn't work.

下面的脚本。

$(function(){
    var aT = new AjaxTest();
    aT.getJson();
});

var AjaxTest = function()
{
    this.ajaxUrl = "http://mydeveloperpage.com/sandbox/ajax_json_test/client_reciever.php";

    this.getJson = function(){
        $.getJSON(this.ajaxUrl, function(data){
            $.each(data, function(i, piece){
                alert(piece);
            });
        });
    }
}

您可以找到完全相同的文件的副本http://mydeveloperpage.com/sandbox/ajax_json_test/。

You can find a copy of the exact same file at "http://mydeveloperpage.com/sandbox/ajax_json_test/".

任何帮助将是很大的AP preciated。

Any help would be greatly appreciated.

谢谢!

推荐答案

从文档

  

由于浏览器的安全限制,大多数Ajax的请求都受到了同样的原产地政策; 请求不能成功地从不同的域,子域,或协议检索数据。

脚本和JSONP请求不受同源策略的限制。

Script and JSONP requests are not subject to the same origin policy restrictions.

您会需要使用 JSONP 让过去同源政策。 jQuery的可以使这种无缝(见上述文档页面的其余部分)。

You're going to need to use JSONP to get past the same-origin policy. jQuery can make this seamless (see the rest of the aforementioned documentation page).

阅读全文

相关推荐

最新文章