2012-07-13 62 views
0

我有一個jQuery each循環,它通過三個JSONP網址的列表,但是,它們的隨機數只是因爲錯誤而失敗:Uncaught TypeError: Property 'callback' of object [object Window] is not a function爲什麼隨機數的這些JSONP請求失敗?

它通常是一個或兩個,有時甚至沒有,但它從來都不是全部,我認爲它與異步部分有關,但我不知道如何解決它。

這裏是我的代碼:

var feeds = ["JSONPUrl", "JSONPUrl", "JSONPUrl"]; 

$.each(feeds, function(index, feedUrl) { 
    $.ajax({ 
    type: "GET", 
    url: feedUrl, 
    processData : true, 
    data: { 
     tagmode: "any", 
     jsonp: "callback", 
     callback: "callback" 
    }, 
    jsonpCallback: "callback", 
    dataType: "jsonp", 
    success: function(data) { 
     // Do some DOM manipulations with the data 
    }, 
    error: function(x,y,z) { 
    } 
}); 
});​ 

任何想法?

+4

你必須讓一些代碼,我猜 – Esailija 2012-07-13 20:36:26

+0

能否請您解釋一下嗎? – 2012-07-13 20:53:00

+0

您需要包含正在執行ajax請求的代碼,以便我們可以爲您提供幫助。你提到一個jQuery'each'循環......在你的文章中包含該循環。 – MrOBrian 2012-07-13 20:56:24

回答

2

由於硬編碼的callback屬性,您的URL看起來像?callback=callback。除去這個,jQuery將自動爲JSONP回調定義具有隨機名稱的臨時函數。

data: { 
    tagmode: "any", 
    jsonp: "callback",  // <-- Do not forget to remove the trailing comma 
    callback: "callback" // <-- Remove this! 
}, 
jsonpCallback: "callback", // <-- Remove this! 

如果你真的要執行被稱爲callback成功的功能,這包括在success處理。

關於註釋:如果您的服務器預計將被稱爲JSONP回調參數,而不是「回調」「JSONP」,使用:

$.ajax({ 
    ... 
    jsonp: "jsonp" 
    ... 
}); 
// results in http://..../?jsonp=jQuery234254342random2342etc 
+0

仍在做,但回調是由'jsonp'而不是回調定義的。 – 2012-07-13 21:11:05

+0

沒有工作,同樣的錯誤,但現在所有三個 – 2012-07-13 21:18:52

+0

現在我得到:jsonp未定義 – 2012-07-13 21:23:03

相關問題