[英]How does an ajax call returns data to a variable?
function getMDBChanges(syncURL, LastSync, WSName, callback) {
$.ajax({
url: syncURL + WSName,
dataType: "json",
success: function (data) {
callback(data);
},
error: function (model, response) {
Notify("divNoteLeft","Nothing to be Sync'd from the Server with URL " + syncURL + WSName );
}
});
}
This is the code I am running. 这是我正在运行的代码。 This works fine.
这很好。 But still I don't understand what exactly does the success callback does.
但是我仍然不明白成功回调到底是做什么的。
thank you 谢谢
The success
(and error
) callbacks are function references (they point to functions). success
(和error
)回调是函数引用(它们指向函数)。 When jQuery receives a success response to the AJAX request, it executes the function that the function reference points to, and passes it three arguments: the returned response (possibly with some pre-processing done on it based on the specified dataType), a status (as a string), and the jqXhr object that issued the request. 当jQuery收到对AJAX请求的成功响应时,它将执行该函数引用指向的函数,并将三个参数传递给它:返回的响应(可能基于指定的dataType对其进行了一些预处理),状态(作为字符串),以及发出请求的jqXhr对象。
That's all it does: calls a function with some arguments; 这就是它的全部工作:调用带有一些参数的函数;
data
is just the name of a parameter in a function definition, you could call that pretty much whatever you liked. data
只是函数定义中参数的名称,您可以随意调用它。 It doesn't return any data to a variable. 它不会将任何数据返回到变量。
In the callback, 在回调中,
function getMDBChanges(syncURL, LastSync, WSName, callback)
the function call might be a inner function in the callback area. 函数调用可能是回调区域中的内部函数。 For example,
例如,
getMDBChanges("www.xyz.com","21st April","WSName",function(data) {
alert(data);
});
Wherever in your code you are calling the function getMDBChanges you are passing in a callback function which is I assume, processing the returned data. 无论您在代码中的何处调用函数getMDBChanges,都将传入一个我认为是的回调函数,以处理返回的数据。 If you want to know what that function is add a
console.log(callback);
如果您想知道该功能是什么,请添加
console.log(callback);
and you will see exactly what the function is and what it's doing. 您将确切地看到该功能是什么以及它在做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.