-1

我正在尝试用 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
};

那么,我怎样才能得到那个网址呢?

4

3 回答 3

0

利用闭包

requester = function(url){
    $.ajax({
        url : "http://url of my proxy?url=" + escape(url),
        type : "GET",
        data-type : "xml"
    }).done(function(data) {

         alert(url);

    });
};
于 2012-11-14T04:08:59.920 回答
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
};
于 2012-11-14T04:10:12.537 回答
-1

它可以从thisvalue 中获得,即jqXHRobject

dataProcessor = function(data){
    console.log(this.url);
};
于 2012-11-14T04:10:27.007 回答