I call ajax in my script using jquery. For this, I created a separate controller (AjaxController) and in it a certain actionRequest method. jQuery passes a post request there:

jQuery.ajax({ url: location.origin + '/ajax/request', async: false, type: 'POST', data: {'params': params},//масив dataType: 'json', success: function(data) {result = data} }); 

in the script itself is such code:

 $params = Yii::$app->request->get('params') echo $params; 

The problem is that the post-requests to get to the server reluctantly. get going fine, but changing to post- nothing comes. With that I make a request to my script (just pkhp script is not tied to the engine)) - everything goes fine ....

ps I do not know yet how to interpret all this. I add the following actions to the action code:

 $root = $_SERVER['DOCUMENT_ROOT']; $file = fopen($root . '/text.txt', 'w'); fwrite($file, '1'); fclose($file); 

If I transfer a get request, the file in the root of the site is created. if I pass the post, I don’t even want to create the file. It feels like it is interrupted somewhere much earlier and when sending a post request, even my code does not execute ...

  • Where does null output? in success ? and in params are you sure something is being transmitted? - Alexey Shimansky
  • @ Alexey Shimansky, changed the question in accordance with the new information. - Doomek
  • And when changing ajax to type: 'POST', do you remember to write a post in php too? Yii::$app->request-> post ('params') . Show me your params array just in case - Alexey Shimansky
  • @ Alexey Shimansky, of course, I did not forget. and the array is removed, now I transmit only the number 32 there. I put get- everything works. then I change to post - in js and in php, I don’t do anything else - it stops working. This is if I send the controller. if I already call my own php script, then everything with this code is transmitted normally. And I tried both the methods of the class and the $ _POSt array, nothing changes. even pure yii set the same. - Doomek
  • one
    Try to see in the logs what is being said if it is said. - Alexey Shimansky

1 answer 1

Use:

 $id = Yii::app()->request->getPost('id'); // Только post $id = Yii::app()->request->getParam('id'); // Для post и get