6

I'm making a jquery post call like so:

   var t1 = $("#form").serialize();

    $.ajax({
        type: "POST",
        url: "save_test.php",
        data: t1,
        cache: false,
        success: function(data){

            if (data.st) {
                alert("Success");
            }
            else if (data.error) {
                alert("Error");
            }                
        }
    });

My PHP Looks like this for my error test:

$res = new stdClass();

$res->error = 'ERROR SEEN';
echo json_encode($res);
exit();

Why can I not access my json encoded data returned from PHP? I would expect this to trigger my data.error alert.

1 Answer 1

6

use datatype:json in jquery code

or you can use

var d=$.parseJSON(data)

then use d.st

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

1 Comment

That was it, I wasn't parsing the JSON returned from PHP. Thanks!

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.