当前位置: 动力学知识库 > 问答 > 编程问答 >

php - ajax request return value not picked up by jQuery

问题描述:

When i try:

$.post("http://mysite.com/myscript.php",{ email:$('#emailRegister').val(),password:$('#passwordRegister').val()} ,function(data){

if(data=='yes')

{

alert("yes", data);

}

else

{

alert("not yes", data);

}

});

I get: "not yes yes", what actually should be "yes yes". My php part adds a user to a database and returns "yes" when the user is added (which works).

In another part of my script, I use the same technique, but there it works just fine.

Does anybody have an idea what could be wrong? If its necessary, here's more code (I'm developping an iOs app using PhoneGap):

$("#register_form").submit(function(){

if(isEmail($("#emailRegister").val())){

if($("#passwordRegister").val() == $("#repeatPassword").val()){

$.post("http://mysite.com/myscript.php",{ email:$('#emailRegister').val(),password:$('#passwordRegister').val()} ,function(data)

{

if(data=='yes')

{

alert("yesss ",data);

localStorage.setItem("email", $('#emailRegister').val()); //saves to the database, key/value

localStorage.setItem("password", $('#passwordRegister').val()); //saves to the database, key/value

$.mobile.changePage($("#checkPage"),{ transition: "flip"});

}

else

{

alert(data);

}

});

}

else{

alert("The passwords do not match!");

}

}

else

{

navigator.notification.alert('Please enter a valid emailadres. This adress will be used in several years to send your pictures',null,"Invalid email");

}

return false; //not to post the form physically

});

网友答案:

If the alert actually says "not yes yes" then the only possibility is that the data variable contains something that looks like "yes" in the alert but actually is subtly different from "yes" (by maybe containing a space or a linefeed character).

Instead of if (data=="yes") try if (data.match(/yes/)).

分享给朋友:
您可能感兴趣的文章:
随机阅读: