[英]Adding callback parameter to $.ajax with jsonp
I am trying to implement jQuery ajax call for cross domain call using jsonp, and the code is like - 我正在尝试使用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
}
I am confused here with the use of callback. 我在这里对回调的使用感到困惑。 If I remove success attribute of $.ajax and use callback=PopulateCategoryObject in place of callback=?
如果我删除$ .ajax的成功属性,并使用callback = PopulateCategoryObject代替callback =? like -
喜欢 -
$.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);
}
});
the difference it makes is, it returns result like - 所不同的是,它返回的结果是-
PopulateCategoryObject, jQuery172012112959187034678_1376976441013( // data here )
And, the function PopulateCategoryObject is not executed. 并且,不执行功能PopulateCategoryObject。
I am unable to figure it out how to set the callback function here? 我无法弄清楚如何在这里设置回调函数? and why is "jQuery172012112959187034678_1376976441013" getting added here with the result?
为什么将“ jQuery172012112959187034678_1376976441013”添加到结果中?
Thanks In Advance. 提前致谢。
Try 尝试
$.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);
}
});
If you don't want your ajax call to cache: set the "cache"
parameter to false
... otherwise set it to true
. 如果您不想让ajax调用缓存,请:将
"cache"
参数设置为false
,否则将其设置为true
。
Another thing...if you don't want to use "success"
parameter to trigger your callback, try jquery $.deffer
: Read this! 另一件事...如果您不想使用
"success"
参数来触发回调,请尝试使用jquery $.deffer
:阅读本文! : http://learn.jquery.com/code-organization/deferreds/examples/ : http : //learn.jquery.com/code-organization/deferreds/examples/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.