在JavaScript/jQuery中,如何在调用完成后检索ajax调用的url?
我正在尝试用JavaScript编写一个web程序,在这个程序中我必须进行多个ajax调用,我需要弄清楚在对url进行ajax调用之后如何获取url。ajax调用是在一个for循环中进行的,该循环通过URL数组进行循环。因此,ajax请求的代码和处理请求返回的函数如下所示:在JavaScript/jQuery中,如何在调用完成后检索ajax调用的url?,javascript,jquery,Javascript,Jquery,我正在尝试用JavaScript编写一个web程序,在这个程序中我必须进行多个ajax调用,我需要弄清楚在对url进行ajax调用之后如何获取url。ajax调用是在一个for循环中进行的,该循环通过URL数组进行循环。因此,ajax请求的代码和处理请求返回的函数如下所示: requester = function(url){ $.ajax({ url : "http://url of my proxy?url=" + escape(url), type
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
};
那么,如何获取该url呢?利用闭包
requester = function(url){
$.ajax({
url : "http://url of my proxy?url=" + escape(url),
type : "GET",
data-type : "xml"
}).done(function(data) {
alert(url);
});
};
您只需保存它并将其传递给您的函数:
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
};
它可从
此
值获得,该值为
为什么不
函数请求程序(url){…}
?啊,谢谢!!我不确定在以这种方式调用函数时如何传递参数,因为我对web编程还不熟悉,但现在这很有意义,而且它可以工作!:)对正确快速的解决方案投反对票?
dataProcessor = function(data){
console.log(this.url);
};