rvandervort
rvandervort

Reputation: 341

jQuery AJAX success callback : Can I see the headers sent?

I am making an AJAX request via jQuery's $.ajax() method.

I have specified a success callback using :

$.ajax({
    ...
    success: function(data, textStatus, xhr) {
        ...
    }
});

Is it possible to see in the callback what request headers were sent ? For example, to retrieve a specific request header value ?

EDIT: The requirement is that I do not "own" the code calling the AJAX request, only the callback. I need to check in the callback whether a specific header was passed.

e.g.

in their code is :

$.ajax({
    beforeSend: function(xhr) {
            xhr.setRequestHeader('A','B');
    },
    success: rogersCallbackMethod
});

Then in my code I would like to say something like this :

function rogersCallbackMethod(data, textStatus, xhr) {
    if (xhr.getRequestHeader('A') == 'B') {
           ...
    }
}

Upvotes: 4

Views: 2105

Answers (4)

James S
James S

Reputation: 3374

Well, I thought this was a valid question and came across this when trying to solve the problem myself.

You can use the this context. I'm using the url property, but there are a few others.

Here's an example of what solved my problem. (Note that I have control over the AJAX calling code, but not the server-side code, which is where the "bIDold" should be returned from.)

$.post(url, obj, function(data) {
  var bIDold = this.url.match(/bID=(\d*)/)[1];
  var bIDnew = data.bID;

  if (bIDold != bIDnew) { ... }
});

Yours would be something like:

if (this.url.match(/A=([^&]*)/)[1] == 'B') { ...

Upvotes: 1

S P
S P

Reputation: 4643

Check out Fiddler. It works in IE and Firefox.

Upvotes: 0

kgiannakakis
kgiannakakis

Reputation: 104168

Use the beforeSend method to view the XMLHttpRequest object or modify it.

Upvotes: 0

Alex
Alex

Reputation: 7374

Firebug:

http://getfirebug.com/

If you want more detailed information and you're using PHP you can also use FirePHP for server debugging of ajax requests.

http://www.firephp.org/

Upvotes: 2

Related Questions