对 jquery .ajax .done() 函数感到困惑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16076009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Confused on jquery .ajax .done() function
提问by Vince
I'm a bit confused on how to use the ajax .done() function. I'm working on validating a form to check if a user exists in database and though ajax would be the best approach (still learning it). I have a php file that return true if user exists and false if user doesn't exist. How would I pass the boolean into the parameter for the done function?
我对如何使用 ajax .done() 函数有点困惑。我正在验证表单以检查用户是否存在于数据库中,尽管 ajax 将是最好的方法(仍在学习)。我有一个 php 文件,如果用户存在则返回 true,如果用户不存在则返回 false。我如何将布尔值传递给 done 函数的参数?
$(".check").blur(function(){
var username = $(".check").val();
$.ajax({
url: "validateUser.php",
type: "post",
data: "username= " + username
}).done(function(result){
if (result == true){
// User exists
}else{
// User doesn't exist
}
});
});
I hope that made sense. I did my best to explain it.
我希望这是有道理的。我尽力解释了。
采纳答案by dt192
I think it should be result == 'true' as the result is a data string
我认为它应该是 result == 'true' 因为结果是一个数据字符串
I just checked, I am correct, the quotes make it work
我刚刚检查过,我是对的,引号使它起作用
PHP:
PHP:
<?php
if($_POST['username']=='sambob'){echo 'true';}else{echo 'false';}
?>
Javascript
Javascript
username='sambob';
$(".check").blur(function(){
$.post("validateUser.php", { username: username })
.done(function(data) {
if (data == 'true'){
alert("User exists");
}else{
alert("User doesn't exist");
}
});
});
json PHP
json PHP
<?php
if($_POST['username']=='sambob'){echo'{"exists":true}';}
else{echo'{"exists":false}';}
?>
json Javascript
json Javascript
$(".check").blur(function(){
$.post("validateUser.php", { username : username },
function(user){
if (user.exists == true){
alert("User exists");
}else{
alert("User doesn't exist");
}
}, "json");
});
回答by soyuka
On your php side, you should echo some json string
, for example I'll do like this on the validateUser.php
:
在你的 php 端,你应该 echo some json string
,例如我会在validateUser.php
:
//Check your database etc.
$user_exists = array('error'=>false,'user'=>true);
echo json_encode($user_exists);
And than with jQuery :
与 jQuery 相比:
$.ajax({
url: "validateUser.php",
type: "post",
data: "username= " + username,
dataType: "json",
}).done(function(result){
if (result.error == false){
//No errors, check user
if(result.user == true)
alert("Exists"); //now do some stuff
else
alert("User don't exists");
}else{
// There is an error
}
});
回答by Dorjee Karma
Success: It will return the XHR success status, eg: 200
Done: Once XHR get success then it will complete the and return with a return data
Try below code
成功:它将返回 XHR 成功状态,例如:200
完成:一旦 XHR 成功,它将完成并返回返回数据
尝试下面的代码
$.ajax({
type: "POST",
url: Url,
data: dataParameter,
async: true,
success: function(results, textStatus) {
debugger;
console.log("success : " + results);
},
error: function(xhr, status, error)
{
console.log("error : " + xhr.responseText);
}
}).done(function(results) {
debugger;
console.log("done : " + results);
});