javascript函数不能等待Ajax响应

编程入门 行业动态 更新时间:2024-10-25 00:31:51
本文介绍了javascript函数不能等待Ajax响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有被称为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响应

本文发布于:2023-08-07 18:16:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1320830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   javascript   Ajax

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!