AJAX PHP error code without using array

I want to get a PHP script called via AJAX to return with an error code that the jQuery AJAX error: handler will handle. I do not want to use a JSON array - I would like it to be as clean as possible. Can someone point me in the right direction?

Thanks,

James

+4
source share
4 answers

If you want to call the AJAX error handler, just pass something other than 200! Try the following:

 <?php header("HTTP/1.0 404 Not Found"); exit(); ?> 

Remember to do two things:

  • Try sending the correct error code to match HTTP methods. those. if the page throws an error, then you should return 500, etc. Here you can see the link
+12
source

Try the following:

 header("HTTP/1.0 404 Not Found"); exit(); 
+2
source

Ajax transmission should not be JSON. It can be xml, json, script or html. By default, AJAX uses XML (this means X from AJAX). I see that you are using jQuery. If you use the ajax method , you have a dataType parameter that you can change.

As you say this, JSON is one of the cleanest in my opinion, but if you mean simply, you can use text instead.

+1
source

EDIT: jQuery'ajax'statusCode method works, but only in jQuery 1.5

I tried using jquery ajax statusCode ', now works with jquery 1.5 , but didn’t get any results ... but this should be the way ...

if my php script does this:

  //localhost/dev/false.php header("HTTP/1.0 404 Not Found"); exit(); 

and my javascript does this:

 $.ajax({ url:'http://localhost/dev/false.php', statusCode: { 404: function() { alert('page not found'); } } }); 
0
source

Source: https://habr.com/ru/post/1343323/


All Articles