简体   繁体   中英

Jquery Ajax using json is getting a undefined value

$.ajax({                
    type: "POST",
    url: "check.php",
    data: "checkit=" + $("#checkEmail").val(),
    success: function(response){
        $("#userCheck").html(response.status);
        if(response.status == true){
            alert("yay");
        }else{
            alert("dsfds");
        }
    }
}, 'json');

someone here suggested doing my ajax returns using json...

here is my php file returning the json..

$data = {success:true};
    echo json_encode($data);

im getting undefined for the return. Perhaps someone here could point me to my mistake?

You are also missing

dataType: 'json'

and your PHP is wrong:

$data=array('status'=>true);

UPDATE

Here are a few suggestions I would try:

1) 'dataType' (case-sensitive I believe)

2) try using the 'contentType' option as so:

contentType: "application/json; charset=utf-8"

I'm not sure how much that will help as it's used in the request to your post url, not in the response. See this article for more info: http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery-with-aspnet-ajax (It's written for asp.net, but may be applicable)

3) Triple check the output of your post url and run the output through a JSON validator just to be absolutely sure it's valid and can be parsed into a JSON object. http://www.jsonlint.com

Hope some of this helps!

You're referencing a different variable, you're sending success (invalidly at that) but checking status . With your current jQuery, it should be:

echo json_encode(array('status' => true));

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