-2

I find plenty of examples of jquery ajax calls with options success:, error:, and complete:.

But haven't found an explanation of what one should put in the target php source to tell the callback an error occurred.

I know that if I set the ajax call to expect a json return, and don't have the target return a json string, then I get the behavior defined by the error: option. Is there another way, to trigger the error option?

I would also like getting recommendations for references/documentation/tutorials for what the php code to use to respond to an ajax code should be.

Ben Bachrach
  • 107
  • 1
  • 1
    Asking for offsite resources is off topic or StackOverflow. In regards to making ajax execute the error callback, that happens if jQuery has an issue with parsing the response in the expected format, or if the response code is not a success response code (2xx-3xx). So given that, if you want to make it execute the error, you can return a non-2xx or non-3xx response code. – Taplar Sep 26 '18 at 15:01
  • Ref. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status – Taplar Sep 26 '18 at 15:03

3 Answers3

0

By default if a PHP page is OK the HTTP response code is 200.

jQuery success HTTP response codes are :

isSuccess = status >= 200 && status < 300 || status === 304;

(as jQuery source code line 9247 https://code.jquery.com/jquery-3.3.1.js)

PHP side, if you want to tell jQuery that an error append you can use the function header :

header($_SERVER["SERVER_PROTOCOL"]." 400 Bad Request"); header($_SERVER["SERVER_PROTOCOL"]." 403 Forbidden"); ...

For more information about the HTTP status codes you can have a list here : https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

SuperToma
  • 21
  • 5
-1

Check out the following articles and see if they clarify your concerns

Ajax error descibed jQuery.ajax()

error

Type: Function( jqXHR jqXHR, String textStatus, String errorThrown ) A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and cross-domain JSONP requests. This is an Ajax Event.

Also one more article from the community: https://stackoverflow.com/a/6265911/2289769

The PHP response header and data type should be the same as in the Ajax request. Requesting json, PHP should also return json otherwise ajax will fail triggering the error.

Some tutorial that you may check: AJAX Request Handler with PHP

Attila Antal
  • 811
  • 8
  • 17
-1

Php throws errors like: fatal error unexpected T String ... Then in your callback use js search function and check if the word error exists. The best way to do this is handling all errors from your php and then echo your own custom text denoting that error occured, so check for the text in your callback. You can use a function with if else block or try catch block, google try catch tutorial for php if you don't know this.

smacaz
  • 148
  • 8