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."

3
  • 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. Commented Sep 29, 2015 at 1:16
  • Not an exact duplicate, but close: stackoverflow.com/questions/6676976/… Commented Sep 29, 2015 at 1: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. Commented Sep 29, 2015 at 1:18

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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); }

3 Comments

This may work, but it is not how the response codes were defined. See: 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 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.
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 alerts to whatever you want to do instead.
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);
        }
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.