0

I am using some jQuery post request, as so

$.post('url', {data: some_data}, function(data, textStatus, jqXHR) {
   console.log(data);  //to debug
   console.log(data.status == "ok");  //to debug
   ....
});

the url hits some php method which returns with

 echo json_encode(array('status' => 'ok'));
 exit;

the problem is that console.log(data) returns {"status":"ok"} but console.log(data.status == "ok"); throws false. How can it be ?

2 Answers 2

1

$.post() return only one object

$.post('url', {data: some_data}, function(data) {
   data = JSON.parse(data);//convert into JSON if data in string format
   console.log(data);  //to debug
   console.log(data.status == "ok");  //to debug
  ....
});
Sign up to request clarification or add additional context in comments.

Comments

1

I think you should simple use Json decode:

data = JSON.parse(data);

and then you can will be able to make comparison data.status == "ok"

1 Comment

I thought of this too but if this was the case, wouldn't the data object return "{"status":"ok"}" and not just {"status":"ok"} like the OP's scenario?

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.