Jquery Ajax using json is getting a undefined value
$.ajax({
type: "POST",
url: "check.php",
dat开发者_开发技巧a: "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));
精彩评论