douyouchou1085 2014-06-09 20:53
浏览 64
已采纳

两个ajax在同一页面上,但第二个没有得到第一个改变的东西

I have a main page, view.php. On this page, I have two ajax requests to two different pages. For example, I have

<script>
functionName1(someData1,someDiv1)
</script>

in the view.php. What it does is pass the data and div into a jQuery function, which would request another php file (php file 1) and pass in the data to create some HTML and MAKE SOME CHANGE IN THE DATABASE, and then echo out the HTML in the div.

Then somewhere after the above code, I have

<script>
functionName2(someData2,someDiv2)
</script>

in the view.php. It may pass some other data to another php file (php file 2) and create HTML and echo out in someDiv2.

Ajax 1 would make changes in the database and the HTML created by ajax 2 should reflect this change. But the problem is, it won't. The HTML seems like nothing has been changed in the database, but I know it's changed because I check the DB. I visit php file 2 and pass in the data by url, it can create the right HTML. So I don't understand why it won't work when I put those two ajax in the same page (view.php).

One thing I tried is to sleep for a couple of seconds in php file 2, and then it would create the right HTML which would reflect the database changes. So I'm wondering if the problem is the second ajax request is so close to the first one that the changes made by the first one is not saved by the DB (I myself highly doubt this, but what else).

  • 写回答

3条回答 默认 最新

  • dongxun6458 2014-06-09 21:00
    关注

    As the commenter StaticVoid mentions, ajax calls are asynchronous. What is most likely happening is that functionName1 is firing off its ajax call, followed very (very) shortly by functionName2 firing off its ajax call. If you want the second call to happen only after the first one has returned, you will need to either a) make ajax synchronous, or b) put the call to functionName2 inside the callback of the ajax request in functionName1.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大