54

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... }
$.get('/someurl.php',some_data_object, function(data, textStatus, jqXHR) {
   var real_url = ? # <-- How do I get this
})

How can I access the URL that jQuery actually used to make the request? Perhaps some method/property of jqHXR? I couldn't find it in the documentation.

Thanks.

3
  • If all else fails you can return it from the server-side code, but I`m guessing there is a more elegant solution. Commented Mar 29, 2011 at 5:44
  • 1
    Is there a way to find this out before the actual request is made ? Commented Aug 9, 2012 at 23:49
  • @sanz probably worth noting that modern browsers can break on XHR. i.e., developer.chrome.com/devtools/docs/… Commented Apr 8, 2015 at 21:30

6 Answers 6

79

Set a break point in success method, then watch

this.url

is the real url for the request.

Sign up to request clarification or add additional context in comments.

3 Comments

Looks like the solution @Chris was looking for but not a good a solution for the case where you need to report or display the URL to the user.
Instead of a breakpoint, just console.log(this.url); inside the success callback.
Also works for error section. When you need to identify which url caused the problem
17

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
    }
    

1 Comment

This is the best method, in my opinion, with a slight change. Rather than all the machinations with error reporting, you can simply console.log or alert the URL from within the beforeSend function. Works like a charm.
10

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/

1 Comment

Using the $.ajaxPrefilter is a very good advice! +1 for this one! :)
2

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.

Comments

2

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);
});

Comments

1

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;
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.