美元.getJSON不工作跨域与我

$.getJSON does not work across domain with me?

本文关键字:工作 getJSON 美元      更新时间:2023-09-26

以下代码对我不起作用:

$.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);
             }
         });

这里是我传递的url: http://www.somedomain.com/page.aspx?sid=6BB5B614-4C43-45DF-BA7D-47A71F0753EF&jsoncallback=?这是返回的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]
 ]
}
我做错了什么?!

getJSON就像任何其他受同源策略影响的ajax请求一样,禁止跨域ajax请求。

你可以通过JSONP来解决这个问题。

编辑:


我只是读getJSON应该做一个自动回退到json,如果URL包含一个callback -参数:

JSONP

如果URL包含字符串"callback=?"(或类似的,由(服务器端API),请求被当作JSONP处理。看到$.ajax()中jsonp数据类型的详细讨论。

因为你有一个jsoncallback参数这一切可能工作-但你发布的响应不使用这个回调函数(也许是因为你只是给?作为回调名称),所以这可能是整个问题:张贴一个函数名作为回调,并实现该函数在你的一边。