Wordpressor
Wordpressor

Reputation: 7553

AJAX status 200, but responses status is "fail"?

I've been working with Ruby on Rails for the last couple of years, moved to project written in PHP and I can't get one thing straight.

I have a jQuery (CoffeeScript) AJAX call, which waits for success or error.

The problem is, I'm getting status ok ALWAYS even if the request fails, see:

enter image description here

Every time I get this it's not success or error, the script just hangs.

$.ajax
  url:  url
  type: type

  beforeSend: ->
    // do something

  error: ->
    // I can never get here

  success: (data) ->
   // this works sometimes, but not when I get this weird stuff from screenshot

I'm wondering if this is a front-end or back-end / server issue, shouldn't I get different status code here?

Upvotes: 0

Views: 230

Answers (2)

Marc B
Marc B

Reputation: 360872

As you've found, ajax goes off the HTTP status code. Your response came in with a 200, therefore the request succeeded. The fact that the body says "failure" is irrelevant. jquery isn't smart and can't "read" what's in the body.

In real-world terms, your ajax request drove to the store, tried to buy a chocolate bar, and the store was out of stock. The trip to/from the store was entirely successful (car didn't break down, you didn't get lost, etc...). The fact you didn't get the chocolate bar has nothing to do with the trip's success/failure.

Upvotes: 1

Erix
Erix

Reputation: 7105

you're just calling some service that returns JSON data, and it is returning you a message that says

status:fail

But you are successfully reaching the server, calling the service method, and getting the response, so 200 OK is not an unreasonable HTTP code. This probably isn't completely RESTful but it's not uncommon.

Upvotes: 1

Related Questions