Ajax request is not asynchronous

I have ajax problem:

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

If I loop 6 times (in my foreach loop), I should have 6 asynchronous requests made to the server. But ajax calls in this case are called synchronously, not asynchronously. Does anyone know why this is happening?

+3
source share
4 answers

Well thank you. After several hours of analysis and reflection, I realized why this script runs synchronously: I open the script.php file, and I notice this and the beginning of the file:

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

.......
?>

So, I have concurrent ajax calls for a php script that uses a session, but the sessions in this case block calls that will be executed synchronously due to a vars session request, so the solution to this problem:

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

.......
?>

session_write_close script, ajax- . http://konrness.com/php5/how-to-prevent-blocking-php-requests/

+8

id script, ..

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
}
+1

async true?

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

, , ?

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

.......
?>

, , albanx,

+1

Source: https://habr.com/ru/post/1790017/


All Articles