I have an ajax problem:
foreach(ids as id){
$.ajax({
url:\'script.php\',
data:\'id=\'+id,
cache:false,
});
}
If I loop 6 ti
<?php
session_start();
$var1=$_SESSION['SOMEVAR'];
//get all session var
......
session_write_close();//then close it
.......
//do php script.....
.......
?>
That's awesome, it solved my issue the same as albanx, thanks
Why not sending all id's to the script and then loop them is faster en more accurate..
Javascript:
// you can send the whole array in once i think not for sure
$.ajax({
url:'script.php',
type: 'POST',
data: ids,
cache:false,
success:function(msg)
{
// when done
}
});
script.php:
foreach($_POST as $id)
{
[............] // do your thing
}
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.....
.......
?>
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.....
.......
?>
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/
Have you tried with setting the async parameter to true?
foreach(ids as id)
{
$.ajax({
url:'script.php',
async: true,
data:'id='+id,
cache:false,
});
}
How have you confirmed that the calls are synchronous and not async?