ajax returns true, but == returns false

Hossam

I have a form and I send it to php to insert into mysql db by ajax. everything went good, and php return the "true" value, but in ajax it shows false message.

here you can look at the php code:-

if(isset($result)){
    $value =  array('msg' => 'true' );
} else {
    $value =  array('msg' => 'false' );
}
echo json_encode($value);

and this is the ajax code:-

success: function(value){
    if (value.msg == 'true') { 
        alert("Saved");
    }else{
        alert("Something went wrong.");
    }

and from console:

//value = "↵{"msg":"true"}

P.S. I don't know what is the ENTER sign "↵" do in the returned value, but I think it shouldn't affect, right?

Anant Singh---Alive to Die

You need to add:-

dataType:"json",

In your Ajax and it will work.

One more solution is $.parseJSON (if you are not using dataType:"json"):-

success: function(value){
  var json = $.parseJSON(value);
   if (json.msg == 'true') { 
        alert("Saved");
    }else{
        alert("Something went wrong.");
    }
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related