I
I
IIISpikerIII2014-02-02 14:08:36
JavaScript
IIISpikerIII, 2014-02-02 14:08:36

Why does the browser hang when canceling an AJAX request in Yii?

Hello! I am writing a request to the controller action in the AJAX view, a long request with sleep () is executed in the controller, the problem is that if you refresh the page or go to another page of the site while the php script is running, then everything will hang until the php execution ends script.

I tried to execute this AJAX request to a separate PHP file where I put sleep and also refresh the page, then everything works fine. It turns out that you need to somehow execute long requests to the action differently. Tell me in which direction you need to dig?

in code view

Yii::app()->clientScript->registerScript('loading', " 
var timestamp = 0;
function WaitMessage()
    {
        $.ajax({
            type: 'POST',
            url: '/index.php/message/ajax',
            data: {send:".$id.",timestamp:timestamp},
            async: true,
            cache: false,
            success: function(data){     
                   $('#message_container').html(data);
            },
        });
    }
WaitMessage();
  ", CClientScript::POS_READY);


in action
public function actionAjax()
  { 
    if(Yii::app()->request->isAjaxRequest && isset($_POST['send'])){
      //пока не пройдет 20сек ничего сделать на сайте нельзя
      sleep(20);
      echo('Прошел!');
    }
  }


So far, I see a solution in creating a separate controller for ajax requests.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rowdy Ro, 2014-02-02
@rowdyro

One possible problem: Yii locks the session file. Accordingly, until php releases it, other requests are not possible.
Do Yii::app()->session->close() before long query, after doing Yii::app()->session->open().
In general, it is better to send long requests to the job server, for asynchronous operation.

I
IIISpikerIII, 2014-02-04
@IIISpikerIII

Thanks for the help and it did! About job of the server did not hear, I esteem.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question