简体   繁体   English

$ .getJSON无法在我跨域使用吗?

[英]$.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= ? 这是我传递的网址: http : //www.somedomain.com/page.aspx ?sid=6BB5B614-4C43-45DF-BA7D-47A71F0753EF&jsoncallback =吗? And here is the JSON that returned back 这是返回的JSON

{"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. getJSON就像受相同来源策略影响的任何其他AJAX请求一样,禁止跨域ajax请求。

you can work around this doing JSONP . 您可以通过执行JSONP解决此问题。

EDIT: 编辑:
i just read getJSON should do an automatic fallback to jsonp if the URL contains a callback -parameter: 我只是读getJSON应该做自动回退到jsonp如果URL包含一个callback -parameter:

JSONP JSONP

If the URL includes the string "callback=?" 如果URL包含字符串“ callback =?” (or similar, as defined by the server-side API), the request is treated as JSONP instead. (或类似,由服务器端API定义),该请求将被视为JSONP。 See the discussion of the jsonp data type in $.ajax() for more details. 有关更多详细信息,请参见$ .ajax()中有关jsonp数据类型的讨论。

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. 因为您有一个jsoncallback -parameter,这一切可能都有效-但您发布的jsoncallback不使用此回调函数(可能是因为您只是将?用作回调名),所以这可能是整个问题:post一个函数名作为回调,并在您这边实现该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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