Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
385 views
in Technique[技术] by (71.8m points)

javascript - Jquery getJSON cross domain problems

I cant seem to get my JSON file to work when pulling it in from another domain using JQuerys getJSON. I have placed the callback part at the end of the url but still have no joy. Firebug tells me its a cross domain issue, which seems to make sense as if I place the json file locally the below code (excluding the ?jsoncallback=? works fine)

Heres the Jquery part

$.getJSON("http://anotherdomain/js/morearticles.js?jsoncallback=?",
    function(json){
        if (show5More.nextSetCount < json.items.length) { // Check not on last group of data
            $('#lineupswitch li').hide();  // Hide the existing items    
            $.each(json.items, function(key,value){ // Loop over the returned data from the json file
                if (key === show5More.nextSetCount) {  // If the itteration is equal to the datablock continure
                     show5More.nextSetCount = show5More.nextSetCount + 1; // 
                     $(value).each( function(index) {
                         if( (index % 2) == 0) {
                             $('<li class="even ' + this.cname +'"><a href="' + this.href + '" class="lineup-thumb"><img src="' + this.thumbimg + '" /></a><h3><a href="' + this.href + '">' + this.titletext + '</a></h3><p>' + this.paratext + '</p></li>').appendTo("#lineupswitch");
                          } else {
                              $('<li class="odd ' + this.cname +'"><a href="' + this.href + '" class="lineup-thumb"><img src="' + this.thumbimg + '" /></a><h3><a href="' + this.href + '">' + this.titletext + '</a></h3><p>' + this.paratext + '</p></li>').appendTo("#lineupswitch");                              
                          } 
                     });
                     return false;
                }
            });
        }
    });         
}

And the JSON, which I have validated.

{ 
  "items": [
            [
                {
                    "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045",
                    "titletext": "Cannabis plants found in house with neglected children",
                    "paratext": "A court has heard four young children lived in",
                    "cname": ""
                },
                {
                    "href": "/edinburgh/video/news-090414-s2-waverley-station-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19537855001_asset-1239732920496.jpg?pubId=1486976045",
                    "titletext": "Multi-million pound revamp for Waverley Station",
                    "paratext": "Edinburgh's Waverley Station is set for a",
                    "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-s2-natal-20090408/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18948154001_asset-1239206353135.jpg?pubId=1486976045",
                  "titletext": "Stillbirth charity on the road to raise awareness", 
                  "paratext": "SANDS Lothian are hoping to highlight their",
                  "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-090407-l2-rbs/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045",
                  "titletext": "Thousands of jobs to go at Royal Bank of Scotland", 
                  "paratext": "Edinburgh-based bank to cut 4,500 positions in the",
                  "cname": ""
                },
                {
                    "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045",
                    "titletext": "1",
                    "paratext": "A court has heard four young children lived in",
                    "cname": "lastlineup"
                }               
            ],

            [
                {
                    "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045",
                    "titletext": "1",
                    "paratext": "A court has heard four young children lived in",
                    "cname": ""
                },
                {
                    "href": "/edinburgh/video/news-090414-s2-waverley-station-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19537855001_asset-1239732920496.jpg?pubId=1486976045",
                    "titletext": "2",
                    "paratext": "Edinburgh's Waverley Station is set for a",
                    "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-s2-natal-20090408/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18948154001_asset-1239206353135.jpg?pubId=1486976045",
                  "titletext": "Stillbirth charity on the road to raise awareness", 
                  "paratext": "3",
                  "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-090407-l2-rbs/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045",
                  "titletext": "Thousands of jobs to go at Royal Bank of Scotland", 
                  "paratext": "4",
                  "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-090407-l2-rbs/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045",
                  "titletext": "Thousands of jobs to go at Royal Bank of Scotland", 
                  "paratext": "Edinburgh-based bank to cut 4,500 positions in the",
                  "cname": "lastlineup"
                }               

            ]   

        ]
}

{ 
  "items": [
            [
                {
                    "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045",
                    "titletext": "Cannabis plants found in house with neglected children",
                    "paratext": "A court has heard four young children lived in",
                    "cname": ""
                },
                {
                    "href": "/edinburgh/video/news-090414-s2-waverley-station-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19537855001_asset-1239732920496.jpg?pubId=1486976045",
                    "titletext": "Multi-million pound revamp for Waverley Station",
                    "paratext": "Edinburgh's Waverley Station is set for a",
                    "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-s2-natal-20090408/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18948154001_asset-1239206353135.jpg?pubId=1486976045",
                  "titletext": "Stillbirth charity on the road to raise awareness", 
                  "paratext": "SANDS Lothian are hoping to highlight their",
                  "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-090407-l2-rbs/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045",
                  "titletext": "Thousands of jobs to go at Royal Bank of Scotland", 
                  "paratext": "Edinburgh-based bank to cut 4,500 positions in the",
                  "cname": ""
                },
                {
                    "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045",
                    "titletext": "1",
                    "paratext": "A court has heard four young children lived in",
                    "cname": "lastlineup"
                }               
            ],

            [
                {
                    "href": "/edinburgh/video/news-090415-s2-squalor-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19721015001_asset-1239819553334.jpg?pubId=1486976045",
                    "titletext": "1",
                    "paratext": "A court has heard four young children lived in",
                    "cname": ""
                },
                {
                    "href": "/edinburgh/video/news-090414-s2-waverley-station-edinburgh/", 
                    "thumbimg": "http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_19537855001_asset-1239732920496.jpg?pubId=1486976045",
                    "titletext": "2",
                    "paratext": "Edinburgh's Waverley Station is set for a",
                    "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-s2-natal-20090408/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18948154001_asset-1239206353135.jpg?pubId=1486976045",
                  "titletext": "Stillbirth charity on the road to raise awareness", 
                  "paratext": "3",
                  "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-090407-l2-rbs/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045",
                  "titletext": "Thousands of jobs to go at Royal Bank of Scotland", 
                  "paratext": "4",
                  "cname": ""
                },
                {
                  "href": "/edinburgh/video/news-090407-l2-rbs/", 
                  "thumbimg":"http://brightcove.vo.llnwd.net/d7/unsecured/media/1486976045/1486976045_18827378001_asset-1239110600777.jpg?pubId=1486976045",
                  "titletext": "Thousands of jobs to go at Royal Bank of Scotland", 
                  "paratext": "Edinburgh-based bank to cut 4,500 positions in the",
                  "cname": "lastlineup"
                }               

            ]   

        ]
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

altCognito is correct. Here is a working example:

Put this in the top of your page you are calling from, in the

$.getJSON("http://www.yourotherdomain.com/testcross.php?jsoncallback=?",
  function(data) {
    $('body').html(data.name).css("color", "green");
  }
);

and the php that would return stuff:

$data = '{"name" : "hello world"}';
echo $_GET['jsoncallback'] . '(' . $data . ');';

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...