Ajax请求不是异步 [英] Ajax request are not async

查看:141
本文介绍了Ajax请求不是异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Ajax的问题​​:

I have an ajax problem:

foreach(ids as id){
  $.ajax({
    url:'script.php',
    data:'id='+id,
    cache:false,
  });
}

如果我循环6次(在我的foreach循环),我应该正在向服务器6异步请求。但在这种情况下,Ajax调用是同步调用,而不是异步。任何一个有任何想法,为什么出现这种情况?

If I loop 6 times (in my foreach loop) I should have 6 asynchronous requests being made to the server. But the ajax calls in this case are called synchronously, and not asynchronously. Any one have any idea why this happens?

推荐答案

确定感谢。几个小时分析和反映后,我明白了为什么这个剧本要syncronsly:我打开script.php的文件,我注意到这一点,该文件的beginig:

Ok thanks. After some hours of analysing and reflecting I realized why this script goes syncronsly: I open the script.php file and I notice this and the beginig of the file:

<?php
session_start();
$var1=$_SESSION['SOMEVAR'];
.......
//do php script.....

.......
?>

所以,我有平行AJAX调用使用会话PHP脚本,但在这种情况下会锁定要执行的会话瓦尔请求syncrosnly事业的来电,所以这个问题的解决方案是:

So I have parallel ajax calls to a php script that uses session, but sessions in this case locks the calls to be executed syncrosnly cause of the session vars request, so the solution of this problem is:

<?php
session_start();
$var1=$_SESSION['SOMEVAR'];
//get all session var
......
session_write_close();//then close it
.......
//do php script.....

.......
?>

通过session_write_close我有我的脚本,使异步方式的Ajax调用。这里一个很好的解释<一个href="http://konrness.com/php5/how-to-$p$pvent-blocking-php-requests/">http://konrness.com/php5/how-to-$p$pvent-blocking-php-requests/

With session_write_close i have my script to make the ajax calls in async way. a good explain here http://konrness.com/php5/how-to-prevent-blocking-php-requests/

这篇关于Ajax请求不是异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆