2013-08-20 39 views
0

我試圖實現使用JSONP jQuery的Ajax調用跨域調用,代碼如下 -

$.ajax({ 
    async:true, 
    cached:true, 
    url: 'cfcs/TempRepository.cfc?method=getAllCategories' 
     +'&storeID='+ storeId 
     +'&callback=?', 
    type: 'get', 
    data: '', 
    dataType: 'jsonp', 
    success: PopulateCategoryObject, 
    error: function (xhr, status, error) { 
     console.log(xhr + ',' + status + ',' + error); 
    } 
}); 

function PopulateCategoryObject(results) { 
    //populate the categories 
} 

我很困惑在這裏與使用回調。 如果我刪除$ .ajax的成功屬性並使用callback = PopulateCategoryObject來代替回調=?像 -

$.ajax({ 
    async:true, 
    cached:true, 
    url: 'cfcs/TempRepository.cfc?method=getAllCategories' 
     +'&storeID='+ storeId 
     +'&callback=PopulateCategoryObject', 
    type: 'get', 
    data: '', 
    dataType: 'jsonp', 
    error: function (xhr, status, error) { 
     console.log(xhr + ',' + status + ',' + error); 
    } 
}); 

它使,則它返回結果一樣的區別 -

PopulateCategoryObject, jQuery172012112959187034678_1376976441013(// data here) 

而且,不執行功能PopulateCategoryObject。

我無法弄清楚如何在這裏設置回調函數?爲什麼在這裏添加「jQuery172012112959187034678_1376976441013」的結果?

在此先感謝。

+0

爲什麼你需要刪除成功的財產? – BorisD

+0

我想直接調用「PopulateCategoryObject」,而不是通過成功,因爲每次將「jQuery172012112959187034678_1376976441013」添加到url並且ajax調用都沒有在此緩存。 保持回調= PopulateCategoryObject,可以保持它的緩存。我不確定。 – Ashmah

回答

1

嘗試

$.ajax({ 
    cached:true, 
    url: 'cfcs/TempRepository.cfc?method=getAllCategories' + '&storeID=' + storeId, 
    jsonpCallback: 'PopulateCategoryObject', 
    dataType: 'jsonp', 
    error: function (xhr, status, error) { 
     console.log(xhr + ',' + status + ',' + error); 
    } 
}); 
+0

嗨Arun,它返回PopulateCategoryObject(//結果),但函數沒有被調用,還有什麼我們需要設置? – Ashmah

+0

browswer控制檯中的任何其他錯誤 –

+0

不,沒有任何其他錯誤! – Ashmah