简体   繁体   中英

$.getJSON does not work across domain with me?

the following code does not work with me:

$.getJSON(url,
         function (data) {
             if (data.results[0]) {
                 alert('hi');

                 $.each(data["Rows"], function (i, el) {
                     $("#target").append("<a href=\"" + el[1] + "\">" + el[2] + "</a><br />");
                 })

                 //container.html(data);
             } else {
                 var errormsg = '<p>Error: could not load the data.</p>';
                 container.html(errormsg);
             }
         });

And here is the url that I pass: http://www.somedomain.com/page.aspx?sid=6BB5B614-4C43-45DF-BA7D-47A71F0753EF&jsoncallback= ? And here is the JSON that returned back

{"Columns":["id","Article_Url","Article_Title","date","num"],
 "Rows":[
   ["5bb93b83-d129-4ca9-8999-ed54910b824d","97.74.67.146/test.html","test","\/Date(1316189236173)\/",25],
   ["82d62b61-d96b-489a-ae91-008b897db553","97.74.67.146/testx.html?xx=x","test","\/Date(1316256259490)\/",11],
   ["97aaf346-1146-429e-bc5a-fcfa4b2d934b","97.74.67.146/testx.html","test","\/Date(1316255702510)\/",4],
   ["2fea1222-e254-4db9-a68e-5129a0d87e8e","97.74.67.146/qn_news_story_s.asp?storyid=1093442005","Oman invests USD3.8b in constructing dry dock","\/Date(1316188504010)\/",2],
   ["82fe900d-eefe-4540-87a4-1fe6057234a7","http://www.menafn.com/qn_news_story.asp?StoryId={83510500-a24b-4f87-9bf1-3985134ee4b6}","Title 1","\/Date(1315411910897)\/",1],
   ["217f8e33-8723-4de3-9afc-438d7172f90e","http://www.menafn.com/qn_news_story_s.asp?StoryId=1093437815&src=MOEN","Title 1","\/Date(1315411969900)\/",1]
 ]
}

What Im doing wrong?!

getJSON is just like any other AJAX -request affected by the same-origin-policy wich forbids doing cross-domain ajax-requests.

you can work around this doing JSONP .

EDIT:
i just read getJSON should do an automatic fallback to jsonp if the URL contains a callback -parameter:

JSONP

If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

since you're having a jsoncallback -parameter this all might work - but the resonse you posted doesn't use this callback-function (maybe because you're just giving ? as callback-name) so this might be the whole problem: post a functionname as callback and implement that function on your side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM