2012-03-27 35 views
0

我想從外部URL獲取一些JSON。這對於dojo文檔中的示例中的twitter請求可以正常工作。dojo.io.script.get中的回調函數

我想問題出現在回調函數中,因爲我不確定是否需要一個以及在哪裏放置它。

的JSON的樣子:

{"upcoming":[]} 

,這就是我怎麼稱呼它:

 function getJSON(){ 
    // Look up the node we'll stick the text under. 
    var targetNode = dojo.byId("results"); 

    // The parameters to pass to xhrGet, the url, how to handle it, and the callbacks. 
    var jsonpArgs = { 
    url: url, 
    load: function(data){ 
     console.log(data); 
     // Set the data from the search into the viewbox in nicely formatted JSON 
     targetNode.innerHTML = "<pre>" + dojo.toJson(data, true) + "</pre>"; 
    }, 
    error: function(error){ 
     targetNode.innerHTML = "An unexpected error occurred: " + error; 
    } 
    }; 
    test = dojo.io.script.get(jsonpArgs); 
} 
dojo.ready(getJSON); 

螞蟻是這樣的輸出我得到:

JSON: 
{ 
    "returnValue": true, 
    "timeStamp": 1332858447300, 
    "eventPhase": 0, 
    "target": null, 
    "defaultPrevented": false, 
    "srcElement": null, 
    "type": "load", 
    "cancelable": false, 
    "currentTarget": null, 
    "bubbles": false, 
    "cancelBubble": false 
} 

回答

0

你指的這個說法在你的例子?

// The parameters to pass to xhrGet, the url, how to handle it, and the callbacks. 

如果是這樣,「回調」是負載和錯誤函數。

+0

我參考了dojo.io.script中描述的回調函數 好吧,我認爲問題是服務器關於json數據的缺少回調函數。如果我正確地理解了這些事情,我必須從服務器收到一個回叫函數,其中包含JSON數據,對嗎? 如果這是正確的,我有問題的解決方案:) – knacker123 2012-03-27 19:30:10