1

I have a php script which returns either a success or error. the success actually returns an empty array, but it allows the success calls in javascript to occur.

I know that i can use a header to do errors, such that the error will fire, but how do i return it custom strings?

example of success:

header("Content-Type: application/json");
echo json_encode(array());

Example of error i was trying.:

header("Wrong Pasword.", true, 400);
echo "Wrong Password.";  

Neither the first arg of header nor the echo is returned to the client side. I am trying to return a custom error so that way i can, in javascript say:

$.ajax({
   url:"script.php", 
   success:function(){},
   error: function(error){ 
       alert(error);
   }});

in this sample, i was trying to have error be: "Wrong Password."

Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • I think you're doing it wrong. The `error` in your ajax call should be used for things like server errors, 404's, etc. The `success` function does not mean that the action you are performing is necessarily successful, but that the request was successful, if that makes sense. In your json include all information, such as whether the action was performed, data returned or any error messages, etc. This is not what the http headers are for. – Mike Sep 29 '15 at 01:16
  • Not an exact duplicate, but close: http://stackoverflow.com/questions/6676976/putting-detailed-rest-error-message-in-http-warning-header-good-bad-idea – Mike Sep 29 '15 at 01:17
  • My first attempt was wrong when it came to the header, for sure., i made an adjustment to say: `header("HTTP/1.0 400 Bad Request"); echo "Wrong Password."; return;` and it will return i believe what works. – Fallenreaper Sep 29 '15 at 01:18

3 Answers3

3

In the PHP you want to generate a json response with an error, or success message like so:

PHP file

<?php 
    header("Content-Type: application/json");
    echo json_encode(array("error" => "Wrong password", "success" => false));
?>

and then front side, you want to get the success/error message via your ajax call:

$.ajax({
   url:"script.php", 
   dataType: 'json',
   success:function(data){
      if(!data.success)
      {
         alert(data.error);
      }
   },
   error: function(error){ 
       console.log(error);
   }});

Please note: ajax error is not used for errors sent back via the script, but errors like failing to load the page, or 500s.

0

It seems that if i return this in php:

header("HTTP/1.0 400 Bad Request");
echo "Wrong Password.";
return;

in javascript, i modify the ajax call to:

error: function(error){ alert(error.responseText); }
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • 2
    This may *work*, but it is not how the response codes were defined. See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html 400 code means, "The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications." It's not exactly malformed *syntax*, but a bad password. – Mike Sep 29 '15 at 01:20
  • @Mike how would you propose an error be returned then such that the "Error" function in jquery would get resolved? Or would you reccomend that both ways are in fact success, but you need to check for the *type* of success. – Fallenreaper Sep 29 '15 at 02:19
  • As I mentioned in my comment on your question, everything should be put in the `success` function. `error` is used for when the actual request fails to be sent to the server for whatever reason. randommman's answer seems like the most sensible approach. Just change the `alert`s to whatever you want to do instead. – Mike Sep 29 '15 at 19:12
0

You can use the below to catch header data

$.ajax({
       url:"script.php", 
       success:function(e){},
       error: function(e){ 
          alert(e.status);
          alert(e.error);
          alert(e.responseText);
          alert(e.statusText);
        }
    });
jlocker
  • 1,478
  • 1
  • 12
  • 23