簡體   English   中英

$ .getJSON無法在我跨域使用嗎?

[英]$.getJSON does not work across domain 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);
             }
         });

這是我傳遞的網址: 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應該做自動回退到jsonp如果URL包含一個callback -parameter:

JSONP

如果URL包含字符串“ callback =?” (或類似,由服務器端API定義),該請求將被視為JSONP。 有關更多詳細信息,請參見$ .ajax()中有關jsonp數據類型的討論。

因為您有一個jsoncallback -parameter,這一切可能都有效-但您發布的jsoncallback不使用此回調函數(可能是因為您只是將?用作回調名),所以這可能是整個問題:post一個函數名作為回調,並在您這邊實現該函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM