How to trigger jquery.ajax() error callback based on server response, not HTTP 500?

后端 未结 6 1153
清酒与你
清酒与你 2020-11-30 03:32

By using jquery ajax function, I can do something like:

$.ajax({

  url: url,
  type: \'GET\',
  async: true,
  dataType: \'json\',
  data: data,

 success:          


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 04:17

    The error callback is for when the Ajax round-trip could not be completed successfully, not something based on your server logic. It would be your responsibility to check for what you consider to be a successful response inside the success callback. i.e. Add a single IF conditional that checks if the message = "There was an error." and include your error logic there, otherwise do the success logic.

    An approach to do what you ask for would be to have the server return a 404 header when there is an error, then it would be handled by the error callback. The problem with this approach is that you'd be hiding if there are actual errors, and you wouldn't be able to pass additional data with it.

提交回复
热议问题