Answer the question
In order to leave comments, you need to log in
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);
public function actionAjax()
{
if(Yii::app()->request->isAjaxRequest && isset($_POST['send'])){
//пока не пройдет 20сек ничего сделать на сайте нельзя
sleep(20);
echo('Прошел!');
}
}
Answer the question
In order to leave comments, you need to log in
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.
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 questionAsk a Question
731 491 924 answers to any question