0

How can I get data from $.ajax Example:

var ajax = $.ajax({
  url: 'http://example.com',
  type: 'GET',
  dataType: 'json'
});

ajax.url // undefined
// But I need
ajax.url // http://example.com
1

2 Answers 2

1

You can't. jqXHR objects, such as ajax, don't have properties for (much of?) the request options in the settings object passed to $.ajax().

But, you can keep settings separately and retrieve request info from it as needed:

var settings = {
  url: 'http://example.com',
  type: 'GET',
  dataType: 'json'
};

var ajax = $.ajax(settings);

console.log(settings.url);

And, if needed, you can attach settings to ajax yourself:

// ...

var ajax = $.ajax(settings);
ajax.settings = settings;
console.log(ajax.settings.url);
Sign up to request clarification or add additional context in comments.

1 Comment

So in a sentence, the answer is "No, without modifying the code creating the XHR you can't" ? I see you've edited your answer, you can make the top part more decisive. The answer indeed is "no". james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.ajax see the jqXHR bit :) +1 and cheers.
0

It was needed for test so I did this way

  var ajax = $.ajax({
    url: '/signin',
    data: { user: 'ButuzGOL' },
    type: 'POST',
    dataType: 'json'
  });
  ajax.abort();
  ajax.always(function() {
    expect(this.type).to.be('POST');
    expect(this.url).to.be('/signin');
    expect(this.data).to.be({ user: 'ButuzGOL' });

    done();
  });

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.