Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can I not see JSON data returned from PHP with jquery post?

Tags:

jquery

php

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.

like image 906
Paul Avatar asked Oct 05 '12 17:10

Paul


1 Answers

use datatype:json in jquery code

or you can use

var d=$.parseJSON(data)

then use d.st

like image 159
StaticVariable Avatar answered Nov 07 '22 11:11

StaticVariable