我进行了一系列异步AJAX调用,并将响应转储到文本文件中。现在,如果我想确定从该文件中的所有AJAX调用获得的响应,我没有唯一的标识符。那么有没有办法在AJAX调用中发送自定义参数并在响应时获得相同的信息,以便所做的调用对于该AJAX请求是唯一的?
$.ajax({
type: "POST",
url: postUrl, // Location of the service
data: postData, //Data sent to server
contentType: "application/json", // content type sent to server
crossDomain: true,
async: true,
password : attr,
success: function(data,success) {
}
});
答案 0 :(得分:2)
您可以添加到对象,如下所示:
$.ajax({
type: "POST",
url: postUrl, // Location of the service
data: postData, //Data sent to server
contentType: "application/json", // content type sent to server
crossDomain: true,
async: true,
password: attr,
anyPropName: "this your data",
success: function (data, success) {
console.log(this.anyNameHere);
}
});
当您在success函数中调用 this 时,您将获得已为ajax定义的对象。 $就(的 {...} 强>);
答案 1 :(得分:0)
所以,使用上面的代码片段,而不是将其移动到它自己的函数,你可以做这样的事情
...
...
inputIdentifer = 'this is some sort of identifying value';
(function(identifier) {
$.ajax({
type: "POST",
url: postUrl, // Location of the service
data: postData, //Data sent to server
contentType: "application/json", // content type sent to server
crossDomain: true,
async: true,
password: attr,
success: function(data, success) {
console.log(identifier);
}
});
}(inputIdentifier));
...
...
你会看到成功函数有标识符