2

Total beginner with javascript and ajax. I wasted two days could not figure this out...

I've processor.php which contains: echo json_encode('ok');

My ajax submits form to that file and receives response ok.

Now I want to use that ok inside my function:

success: function (data) {
if (data == "ok") {
//do smth
}

Unfortunately this if never evaluates true.

I check alert(data); inside success function and I get "ok"

What is the problem here?

0

2 Answers 2

3

json_encode('ok') is "ok", not ok. Try comparing data == '"ok"', or handle this in some other way.

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

3 Comments

What if I do json_encode(array("status" => "ok")) How do I get array status? alert(data[status]) doesn't work...
@SandroDzneladze it would have to be data['status'] or data.status
Thanks! I used other answer, but upvoted yours too for help! :)
0

change this echo json_encode('ok'); to echo 'ok';

Comments

Your Answer

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