H
H
hollanditkzn2017-06-09 13:57:17
Yii
hollanditkzn, 2017-06-09 13:57:17

How to remove multiple requests in ajax?

I am using ajax in yii2. I implemented that when you click in the gridciew, a block comes out, you can view both the order itself, and you can click on edit and go inside the block to edit. c83897393b3a4b2fa5d8a894effbd620.png
f93c265e42044ccc98069394ef2259e2.pngOnly when you do a lot of editing and again click on another line so that it opens, then too many requests come out and later it slows down and does not work correctly. Another warning comes out

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/ .

In the _zakazold file where the data is stored, here is the ajax request
<?php $this->registerJs('$("body").on("click", "#edit", function(){//нажал на кнопку редактировать, происходит событие
           var key = $(this).data("key");//получаем id заказа
           console.log(key);
        $.ajax({
            url: "'.Url::toRoute(['zakaz/zakazedit']).'?id="+key,//переводим страницу _zakazedit
            success: function(html){
                $(".view-zakaz").html(html);
            }
        })
    });') ?>

<?= Html::button('Редактировать', ['id' => 'edit', 'data-key' => $model->id_zakaz]) ?>

And my code looks like this
<?php $this->registerJs('
$("body").on("click", "#view", function(){//переводит ползователя на просмотр заказа
           var key = $(this).data("key"); //получает id заказа от data-key
        $.ajax({
            url: "'.Url::toRoute(['zakaz/zakazold']).'?id="+key, // открывает данную страницу просмотра _zakazold
            success: function(html){
                $(".edit-zakaz").html(html);
            }
        })
    });
    $("body").on("click", "tr", function(){//если была нажата на другую строку, то информация меняется на другие данные, но есть проблема, то что куда бы не нажал в блоке, но все равно открывает страницу _zakazold
        var key = $(this).data("key");//получаем id заказа
        $.ajax({
            url: "'.Url::toRoute(['zakaz/zakazold']).'?id="+key,
            success: function(html){
                $(".edit-zakaz").html(html);
            }
        })  
    })
    ') ?>
<?php $form = ActiveForm::begin(); ?>
....
<?= Html::submitButton('Сохранить', ['class' => 'btn btn-primary']) ?>
    <?php ActiveForm::end(); ?>
    <?= Html::button('Вернуться', ['id' => 'view', 'data-key' => $models->id_zakaz]) ?>

In the controller
public function actionZakazedit($id){

        $models = $this->findModel($id);
        if($models->load(Yii::$app->request->post()) && $models->save()){
            return $this->redirect(['admin']);// если было нажата кнопка сохранить , то сохраняется данные и редирект на странbцу admin
        } else {
        return $this->renderAjax('_zakazedit', ['models' => $models]);//иначе открывает внутри блока страницу _zakazedit 
        }
    }

    public function actionZakazold($id){

        $model = $this->findModel($id);
            return $this->renderAjax('_zakazold', ['model' => $model]);
    }

How to fix this problem, that is, there may be such a number of requests 1, 3, 5, 10, 15, 25. And so on and then the site starts to slow down.
Here are the requests in the debug yii2
frontend/web/zakaz/zakazold?id =42 135 ms 593a9a293f108
GET 200 /frontend/web/zakaz/zakazold?id=42 85 ms 593a9a29671b1
GET 200 /frontend/web/zakaz/zakazedit?id=42 106 ms 593a9a2a7f4a8
GET 200 /frontend/web/zzakaz? id=42 217 ms 593a9a2aa13a8
GET 200 /frontend/web/zakaz/zakazedit?id=42 355 ms 593a9a2aa13a8
GET 200 /frontend/web/zakaz/zakazedit
? ?id=42 278 ms 593a9a2aa32e9
GET 200 /frontend/web/zakaz/zakazedit?id=42 357 ms 593a9a2abc92e
GET 200 /frontend/web/zakaz/zakazedit?id=42 349 ms 593a9a2acf5fb
GET 200 /frontend/web/zakaz/zakazedit?id=42 354 ms
593a9a2adc11e 593a9a2a869da GET 200 /frontend/web/ zakaz
/zakazold?id=42 248 ms ms 593a9a2c1af8b GET 200 /frontend/web/zakaz/zakazold?id= 45 189 256 ms 593a9a2c178da GET 200 /frontend/web/zakaz/zakazold?id=45 418 ms 593a9a2c103a8

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-06-09
@hollanditkzn

It is impossible to answer your question, you need to look at your code and site. All requests are in the console, see what requests they are, what code launches them, and look for an opportunity to reduce them.
Because it is impossible to understand without a fortuneteller what kind of requests there are, whether they are needed. whether there are duplicates or just not optimally done.
PS: when asking a question, go away from your case and move on to abstractions when explaining. Sometimes it is better to break a question into several questions. And when you introduce concepts like
support with explanations and code. You brought js. but where there "do editing" only you know.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question