Ajax request are not async

后端 未结 4 975
既然无缘
既然无缘 2021-01-06 09:48

I have an ajax problem:

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

If I loop 6 ti

相关标签:
4条回答
  • 2021-01-06 10:13
    <?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

    0 讨论(0)
  • 2021-01-06 10:31

    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
    }
    
    0 讨论(0)
  • 2021-01-06 10:39

    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/

    0 讨论(0)
  • 2021-01-06 10:39

    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?

    0 讨论(0)
提交回复
热议问题