2013-08-20 35 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