我有被称为AJAX功能 flagIt(),这是从内部调用另一个函数叫做 validateForm() 。 validateForm()在提交被执行。
I have an AJAX function called flagIt(), which is called from within another function called validateForm(). validateForm() is executed on submit.
function validateForm(){ var error = ""; //do stuff to validate form if(error == ""){ flagIt(); }else{ window.alert(error); return false; } }
由于上方出现,它会调用 flagIt(),但允许的形式,不等待我的AJAX这是在 flagIt()。
As it appears above, it will call flagIt(), but then allows the form to submit without waiting for the response from my AJAX which is performed within flagIt().
如果我添加一个 window.alert('东西'); 调用 flagIt()后这有时间来执行AJAX和一切正常。
If I add a window.alert('something'); after the call to flagIt() It has time to execute the AJAX and everything works.
我怎么能强迫函数 validateForm()来使形式不添加警报提交之前等待我的AJAX?
How can I force the function validateForm() to wait for my AJAX before allowing the form to submit without adding an alert?
我已经试过返回flagIt()。
修改
要澄清一下,我不是在提交AJAX的形式。我更新的数据库。
To clarify, I am not submitting the form in AJAX. I'm updating a database.
推荐答案只是添加到我的评论
您code不会停止在 flagIt(),因为它的一个AJAX,这是的异步的
Your code does not stop at flagIt() because its an AJAX , which is Asynchronous
如果你想让它停在那里,等待AJAX调用才能完成,你必须将异步标记设置为假
If you want it to stop there and wait for the AJAX call to finish you will have to set the async flag to false
xmlhttpp.open(GET,网址,虚假);
是的,你可以调用 SJAX
更多推荐
javascript函数不能等待Ajax响应
发布评论