admin管理员组文章数量:1026971
this is a part of the check in my form
function check(theform) {
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.username.value)) {
alert("not valid username");
theform.username.focus();
return false;
}
$.ajax({
type: "POST",
url: "username.asp",
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
});
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.password.value)) {
alert("not valid password");
theform.password.focus();
return false;
}
}
for some reason of sync... it check the username then duplicated username with the ajax and not waiting for respond and jump to the password check.
i don't want to insert the rest of the code to isreadystate (or what ever it is) because i might move the username duplicate check to the end... and then the function will end before the ajax anyway
what should i do?
this is a part of the check in my form
function check(theform) {
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.username.value)) {
alert("not valid username");
theform.username.focus();
return false;
}
$.ajax({
type: "POST",
url: "username.asp",
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
});
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.password.value)) {
alert("not valid password");
theform.password.focus();
return false;
}
}
for some reason of sync... it check the username then duplicated username with the ajax and not waiting for respond and jump to the password check.
i don't want to insert the rest of the code to isreadystate (or what ever it is) because i might move the username duplicate check to the end... and then the function will end before the ajax anyway
what should i do?
Share Improve this question edited May 25, 2010 at 10:07 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked May 25, 2010 at 9:21 Y.G.JY.G.J 1,1085 gold badges20 silver badges47 bronze badges2 Answers
Reset to default 6The first A in AJAX stands for "Asynchonous". The call is made, and the execution of the function continued without waiting for the call to return.
You could set the async
option in your call to false
, making your call sychronous. However, you would have to change your functions so the return false
makes it through to the check
function, and it is not remended by the jQuery manual:
The first letter in Ajax stands for "asynchronous," meaning that the operation occurs in parallel and the order of pletion is not guaranteed. The async option to $.ajax() defaults to true, indicating that code execution can continue after the request is made. Setting this option to false (and thus making the call no longer asynchronous) is strongly discouraged, as it can cause the browser to bee unresponsive.
a better way around it would be to make the checks you can make in "real time" within the function, then start the Ajax request, and submit the form in the success callback of that request.
2nd attempt at a syncrhonous call. This seems to actually work - tested in FF 3.6.
var name_success = true;
$.ajax({
type: "POST",
async: false,
url: "username.asp", // Needs to return "EXISTS" in this example
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (username == "EXISTS") { // Changed callback return value -
// safer this way
alert("username already in use");
name_success = false; // should work because
// we're in check()'s scope
return false;
}
}
});
alert (name_success);
your code will first make user name test, if success, it will make ajax call, as the ajax call is asynchronous, the code execution continues to the password check.
the return false inside teh following code does not makes any sense as the success is a call back method when ajax call pletes successfully.
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
Add async: false as shown in the code to make it a synchronous call,
this is a part of the check in my form
function check(theform) {
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.username.value)) {
alert("not valid username");
theform.username.focus();
return false;
}
$.ajax({
type: "POST",
url: "username.asp",
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
});
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.password.value)) {
alert("not valid password");
theform.password.focus();
return false;
}
}
for some reason of sync... it check the username then duplicated username with the ajax and not waiting for respond and jump to the password check.
i don't want to insert the rest of the code to isreadystate (or what ever it is) because i might move the username duplicate check to the end... and then the function will end before the ajax anyway
what should i do?
this is a part of the check in my form
function check(theform) {
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.username.value)) {
alert("not valid username");
theform.username.focus();
return false;
}
$.ajax({
type: "POST",
url: "username.asp",
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
});
var re = /^\w[0-9A-Za-z]{5,19}$/;
if (!re.test(theform.password.value)) {
alert("not valid password");
theform.password.focus();
return false;
}
}
for some reason of sync... it check the username then duplicated username with the ajax and not waiting for respond and jump to the password check.
i don't want to insert the rest of the code to isreadystate (or what ever it is) because i might move the username duplicate check to the end... and then the function will end before the ajax anyway
what should i do?
Share Improve this question edited May 25, 2010 at 10:07 abatishchev 100k88 gold badges301 silver badges442 bronze badges asked May 25, 2010 at 9:21 Y.G.JY.G.J 1,1085 gold badges20 silver badges47 bronze badges2 Answers
Reset to default 6The first A in AJAX stands for "Asynchonous". The call is made, and the execution of the function continued without waiting for the call to return.
You could set the async
option in your call to false
, making your call sychronous. However, you would have to change your functions so the return false
makes it through to the check
function, and it is not remended by the jQuery manual:
The first letter in Ajax stands for "asynchronous," meaning that the operation occurs in parallel and the order of pletion is not guaranteed. The async option to $.ajax() defaults to true, indicating that code execution can continue after the request is made. Setting this option to false (and thus making the call no longer asynchronous) is strongly discouraged, as it can cause the browser to bee unresponsive.
a better way around it would be to make the checks you can make in "real time" within the function, then start the Ajax request, and submit the form in the success callback of that request.
2nd attempt at a syncrhonous call. This seems to actually work - tested in FF 3.6.
var name_success = true;
$.ajax({
type: "POST",
async: false,
url: "username.asp", // Needs to return "EXISTS" in this example
data: "username="+theform.username.value,
success: function(msg){
username = msg;
if (username == "EXISTS") { // Changed callback return value -
// safer this way
alert("username already in use");
name_success = false; // should work because
// we're in check()'s scope
return false;
}
}
});
alert (name_success);
your code will first make user name test, if success, it will make ajax call, as the ajax call is asynchronous, the code execution continues to the password check.
the return false inside teh following code does not makes any sense as the success is a call back method when ajax call pletes successfully.
success: function(msg){
username = msg;
if (!username) {
alert("username already in use");
return false;
}
}
Add async: false as shown in the code to make it a synchronous call,
本文标签: jqueryJavaScript check of a formnot waiting for AJAX responseStack Overflow
版权声明:本文标题:jquery - JavaScript check of a form, not waiting for AJAX response - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745653831a2161477.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论