Now I am doing a project, and when you click on some buttons, the Ajax should work. Example: to rate the "+" article. Clicking on + leaves the AJAX request, but the fact is that in the debugger its time is 1.883 MS and oscillates around it each time. And the matter is not in what is in action, because even if the action only has return true; then the request is executed 1.443 MS, which seems to be too much. Or let's say that a modal window opens, its content opens too somewhere in a second. What could be causing this at all? Here is the code of the example of the execution of the Ajax. There is nothing superfluous, but nothing at all.
$.ajax({ url: '/profile/like', data: {id: id}, type: "POST", success: function(res) { }, error: function() { } }); PS I created a separate test project, so that when you press a button, an AYAX request is sent, and it is sent perfectly. Immediately everything works. And in the project with Yii2, it is just some kind
Controller:
public function actionUnlike() { $photo = Photo::findOne(Yii::$app->request->post()); // ΠΠ°Ρ
ΠΎΠ΄ΠΈΠΌ ΡΠΎΡΠΊΡ Ρ ΡΡΠΈΠΌ ID (Π° ΡΠΎ ΠΏΠ΅ΡΠ΅Π΄Π°Π΄ΡΡ ΡΠ΅ΡΠ΅Π· Π±ΡΡΠΏ ΠΊΠ°ΠΊΠΎΠΉ ΡΠΎ Π»Π΅Π²ΡΠΉ Π°ΠΉΠ΄ΠΈΡΠ½ΠΈΠΊ Π΅ΡΠ΅..) if($photo) Like::unlike(Yii::$app->request->post('id')); // ΠΏΡΠΈΠ½ΠΈΠΌΠ°Π΅Ρ post ID ΡΠΎΡΠΊΠΈ ΠΈΠ· AJAX (main.js) } The static method unlike in the model:
public static function unlike($id) { $like = Like::find()->where(['user_id' => Yii::$app->user->identity->id, 'photo_id' => $id])->one(); return $like->delete(); } Handler inside js
$.ajax({ url: '/profile/unlike', data: {id: id}, type: "POST", success: function(res) { }, error: function() { } }); } Debagger shows that Unlike works for approximately 1,377 ms.
$photo = Photo::findOne(Yii::$app->request->post());- why do you pass all post data to the method? - Blacknife