我正在尝试用JavaScript编写一个Web程序,我必须进行多个ajax调用,并且我需要弄清楚如何在对url进行ajax调用之后获取url。 ajax调用是在for循环中进行的,循环遍历url数组。因此,ajax请求的代码和处理请求返回的函数看起来像这样:
requester = function(url){
$.ajax({
url : "http://url of my proxy?url=" + escape(url),
type : "GET",
data-type : "xml"
}).done(dataProcessor);
};
dataProcessor = function(data){
//a bunch of code, including things where I must have the url for the ajax request
};
那么,我该如何获得该网址?
答案 0 :(得分:0)
利用闭包
requester = function(url){
$.ajax({
url : "http://url of my proxy?url=" + escape(url),
type : "GET",
data-type : "xml"
}).done(function(data) {
alert(url);
});
};
答案 1 :(得分:0)
您可以直接保存并将其传递给您的函数:
requester = function(url){
var fullURL = "http://url of my proxy?url=" + escape(url)
$.ajax({
url : fullURL,
type : "GET",
data-type : "xml"
}).done(function(data) {dataProcessor(data, fullURL)});
};
dataProcessor = function(data, url){
//a bunch of code, including things where I must have the url for the ajax request
};
答案 2 :(得分:-1)
可从this
值获取,即jqXHR
object。
dataProcessor = function(data){
console.log(this.url);
};