Access the URL of an jQuery Ajax Request in the Callback Function

前端 未结 6 1838
暖寄归人
暖寄归人 2020-12-08 18:20

Is there a way that I can see the URL that was requested when I do an Ajax request with jQuery?

e.g.,

var some_data_object = { ...all sorts of junk..         


        
相关标签:
6条回答
  • It seems like the ajaxSend global handler (http://api.jquery.com/ajaxSend/) provides the url in its settings parameter. You could store a mapping from the xhr object to the url in your ajaxSend callback, then in your success callback look it up given the xhr object that it provides you with.

    var mappings = {};
    
    $.ajaxSend(function(event, xhr, settings) {
       mappings[xhr] = settings.url;
    });
    
    $.ajax({
      url: "http://test.com",
      success: function(data, textStatus, xhr) {
        console.log("url = ", mappings[xhr]);
        delete mappings[xhr];
      }
    });
    

    This has the advantage of not having to modify each $.ajax() object.

    0 讨论(0)
  • 2020-12-08 19:01

    Using $.ajaxPrefilter:

    // Make sure we can access the original request URL from any jqXHR objects
    $.ajaxPrefilter(function(options, originalOptions, jqXHR) {
      jqXHR.originalRequestOptions = originalOptions;
    });
    
    $.get(
      'http://www.asdf.asdf'
    ).fail(function(jqXHR){
      console.log(jqXHR.originalRequestOptions);
      // -> Object {url: "http://www.asdf.asdf", type: "get", dataType: undefined, data: undefined, success: undefined}
    });
    

    http://api.jquery.com/jQuery.ajaxPrefilter/

    0 讨论(0)
  • 2020-12-08 19:09

    Set a break point in success method, then watch

    this.url
    

    is the real url for the request.

    0 讨论(0)
  • 2020-12-08 19:10

    Here is a possible solution:

    1. Catch the ajax call before it is sent to the server by implementing the beforeSend callback function.
    2. Save the url and the data
    3. Report it in the error message you generate.

      var url = "";
      
      $.ajax({
        url: "/Product/AddInventoryCount",
        data: { productId: productId, trxDate: trxDate, description: description, reference: reference, qtyInCount: qtyInCount }, //encodeURIComponent(productName)
        type: 'POST',
        cache: false,
        beforeSend: function (jqXHR, settings) {
          url = settings.url + "?" + settings.data;
        },
        success: function (r) {
          //Whatever            
        },
        error: function (jqXHR, textStatus, errorThrown) {
          handleError(jqXHR, textStatus, errorThrown, url);
        }
      });
      
      function handleError(jqXHR, textStatus, errorThrown, url) {
         //Whatever
      }
      
    0 讨论(0)
  • 2020-12-08 19:13

    FYI, as an addition to airbai's comment -I cannot comment inside his answer,- you can add your own data to the call and retrieve it inside the callbacks. This way you don't have to parse the URL.

    In this example JSONP request I have added the variable user_id (tested with jQuery 3.2):

    var request = $.ajax({
        dataType: "json",
        url: "http://example.com/user/" + id + "/tasks?callback=?",
        user_id: id,
        success: function(data) {
            console.log('Success!');
            console.log("User ID: " + this.user_id);
        },
        timeout: 2000
    }).fail(function() {
        console.log('Fail!');
        console.log("User ID: " + this.user_id);
    });
    
    0 讨论(0)
  • 2020-12-08 19:22

    I couldn't find it in the docs either. Maybe just add it to the jqXHR object through a "proxy" wrapper like...

    I haven't tested this, so you may need to call $.param() and concat to the url. See http://api.jquery.com/jQuery.param/

    var myGet = function(url, data, success) {
        $.get(url, data, function(data, textStatus, jqXHR) {
           jqXHR.origUrl = url; // may need to concat $.param(data) here
           success(data, textStatus, jqXHR);
        });
    }
    

    usage:

    var some_data_object = { ...all sorts of junk... }
    myGet('/someurl.php',some_data_object, function(data, textStatus, jqXHR) {
       var real_url = jqXHR.origUrl;
    })
    
    0 讨论(0)
提交回复
热议问题