我正在学习ajax。我需要使用ajax验证表单,以检查json文件(users.json)中是否存在用户选择的伪。我需要获取var checkPseudo的值。 Actualy这个var总是" undefined"。
我认为这是异步开发的经典问题。
注意:我可能不会使用Jquery库。
这是我的代码
<!-- This is my form -->
<form method="post" id="frmInscription" action="adduser.php" onsubmit="return formValidation(this)">
Pseudo :<input name="pseudo" type="text" /><br />
<input type="submit" name="send" value="Submit" />
</form>
<!-- javascript functions to validate the form -->
function makeRequestCheckPseudo(callback){
if(xhr = createXhr()){ // returns an xhr object
xhr.open("GET", "users.json", true);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
//I use a callback function to get the ajax responseText in the function formValidation(frm)
callback.apply(xhr);
return callback(); //I'm not sure if I need to return the callback function
}
}
xhr.send(null);
};
}
function formValidation(form){
var pseudo = form.elements["name"].value;
var checkPseudo = makeRequestCheckPseudo(function(){
var jsonPseudos = JSON.parse(this.responseText); // here I get the datas from users.json by ajax response
if(!jsonPseudos[pseudo]){ //if pseudo is present in the json object properties
alert("pseudo is not present in users.json !");
return true; // I want to send my form
} else {
alert("pseudo is present users.json !");
return false; // I don't want to send my form
}
});
return checkPseudo; // should be "true" or "false" but I get "undefined"
//if checkPseudo is false the form will not be send
//if checkPseudo is true the form will be send
}
答案 0 :(得分:2)
因为这就是异步程序的工作方式。
您可以做的是默认情况下阻止表单提交,如果验证成功则手动调用表单的提交方法
function makeRequestCheckPseudo(pseudo, callback) {
if (xhr = createXhr()) { // returns an xhr object
xhr.open("GET", "users.json", true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
//I use a callback function to get the ajax responseText in the function formValidation(frm)
callback.apply(xhr);
callback(); //I'm not sure if I need to return the callback function
}
}
xhr.send(null);
};
}
function formValidation(form) {
var pseudo = form.elements["name"].value;
var checkPseudo = makeRequestCheckPseudo(function() {
var jsonPseudos = JSON.parse(this.responseText); // here I get the datas from users.json by ajax response
if (!jsonPseudos[pseudo]) { //if pseudo is present in the json object properties
alert("pseudo is not present in users.json !");
form.submit(); //if the validation is successfull then manually trigger the form submit
} else {
alert("pseudo is present users.json !");
}
});
return false; //prevent the default action
}
答案 1 :(得分:1)
您忘记将pseudo
作为第一个参数传递。可能会解决问题。
function formValidation(form){
var pseudo = form.elements["name"].value;
var checkPseudo = makeRequestCheckPseudo(pseudo, function(){
// rest of the code
})
}
如果是这一行:
return callback();//I'm not sure if I need to return the callback function
我猜你需要返回callback()函数。否则,返回值将不会设置为checkPseudo
行:
var checkPseudo = makeRequestCheckPseudo(pseudo, function(){
// rest of the code
}
答案 2 :(得分:1)
试试这个;)
function formValidation(form){
var pseudo = form.elements["pseudo"].value;
if(xhr = createXhr()){ // returns an xhr object
xhr.open("GET", "users.json", true);
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status == 200){
var jsonPseudos = JSON.parse(this.responseText); // here I get the datas from users.json by ajax response
if(!jsonPseudos[pseudo]){ //if pseudo is present in the json object properties
alert("pseudo is not present in users.json !");
return true; // I want to send my form
} else {
alert("pseudo is present users.json !");
return false; // I don't want to send my form
}
}
xhr.send(null);
}
}
尝试使用单个函数,并在此处以错误的方式引用字段:var pseudo = form.elements["name"].value;