简体   繁体   中英

jquery ajax json doesnt return true

i have large form in my website and using serialize() to process the form.

my problem is:
the result always return false after the form has been completed! i checked using firebug. if false, the result being shown. it was actually data.ok == true had been called, but it didnt show the message in the page? and it didnt redirect the page to the destination address?

jquery ajax:

$("#details").live("submit", function(e){
   var form = $(this).serialize();
   var data_string = form;
   $.ajax({
            type: "post",
            url: "../_include/ajax.php?details",
            cache: false,
            data: data_string,
            dataType: "json",
            success: function(data) {
                if(data.ok) {
                    ("#pop").html(data.message).addClass("oke").fadeIn("slow");
                    setInterval(function() {
                        location.href = data.redirect
                    },2000)

                } else {
                    $("#pop").html(data.message).addClass("warning").fadeIn("slow");
                }

            }
        });

        e.preventDefault();
})

in PHP:

if (isset($_GET['details'])) {

    if (empty($name)) {
            $data['ok'] = false;
            $data['message'] = 'Please enter name!';
    } ................ {
            .............
    } else {
      $db->query("UPDATE query....");
      $data['ok'] = true;
      $data['message'] = 'Your details has been submitted!';
      $data['redirect'] = 'index.php?p=details';
    }


echo json_encode($data);
}

you check for GET in your PHP ( if (isset($_GET['details'])) ), but send POST (by specifying the type as post ) in your AJAX.

Either check the $_POST array instead of the $_GET , or change the type to get .

You appear to have a syntax error in your success function (if that's not a copy/paste error):

("#pop").html(data.message).addClass("oke").fadeIn("slow");

should be:

$("#pop").html(data.message).addClass("oke").fadeIn("slow");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM