1

I've a jQuery ajax GET request which gives the server a clientId. When the clientId is invalid I return a 400 "Bad token etc...". This works great only I do not understand why jQuery throws this error in the console?

So my question is: "How can I stop jQuery from throwing and simply just call the error callback instead of both"

My code:

        $.ajax({
            type: type,
            url: url,
            timeout: 30000,
            dataType: "text",
            success: function (data, textStatus, jqXHR) {
                callback(textStatus, url, jqXHR, data);
            },
            error: function (jqXHR, textStatus, errorThrown) {
                callback(textStatus, url, jqXHR, errorThrown);
            }
        });
Haagenti
  • 7,974
  • 5
  • 38
  • 52
  • 3
    `error` is called when http request fails, and that's not the case – k102 Dec 30 '14 at 10:51
  • Probably you're getting some HTML when the response comes, and the browser is probably trying to load an image or other media content and getting a 404... What file is getting the 404? – António Regadas Dec 30 '14 at 10:53
  • A 400 error code reflects a malformed syntax in the data received from the server. It appears the when the clientID does not exist on yor server, a malformed data is returned. Not too sure about your question, but a 400 code should only trigger the success callback. So what you can do is that even when the request is successful you should include information in the returned data whether the server lookup is valid or not, and evaluate it as such in the success callback. – Terry Dec 30 '14 at 10:55
  • Error is called and the browser is not rendering why should it? – Haagenti Dec 30 '14 at 11:01

1 Answers1

0

If you are returning a 400 status code it will be considered as a failed request. check this link

About your console showing the error it is the normal behavior. If you don't want it to show an error you shouldn't return an error code: check this link

Community
  • 1
  • 1
LAPA
  • 136
  • 1
  • 4