0

I'm getting an error when trying to call an REST service using ajax. I'm trying to track down the error. The xhr.message is always coming in as undefined. Is there a better way to get the exact message as to why it is erroring?

$.ajax({
    url: 'myurl.com/post/',
    type: 'post',
    data: query,
    cache: false,
    error: function (xhr, status, error) {
        alert(JSON.stringify(xhr.message));
        alert(JSON.stringify(status));
        alert(JSON.stringify(error));
    }
});
Jon Harding
  • 4,928
  • 13
  • 51
  • 96
  • 1
    But why exactly should it be defined? Neither native XMLHttpRequest nor its superset provided by jQuery doesn't mention any `message` property. – raina77ow Dec 18 '13 at 22:36

1 Answers1

2

First, you should be using the newer (non-deprecated) format to make ajax calls

Deprecation Notice: The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callbacks are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.

Here's an example:

$.ajax({
    url: 'myurl.com/post/',
    type: 'post',
    data: query,
    cache: false
})
.done(function(data) {
    alert(JSON.stringify(data));
})
.fail(function (xhr, status, error) {
    alert(JSON.stringify(xhr.message));
    alert(JSON.stringify(status));
    alert(JSON.stringify(error));
});

Second, message isn't a property (I don't think?) of the $.ajax call; it's statusthat you want, and it's just a text string. Try console.log(status), and same for xhr and error instead, and inspect the console to see what's being returned and what you want to retrieve.

brandonscript
  • 68,675
  • 32
  • 163
  • 220