php – Post parameters in yii via ajax

Question:

calling ajax in my script using jquery. To do this, I created a separate controller (AjaxController) and in it a certain actionRequest method. jQuery passes the 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, there is already the following code:

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

The problem is that post-requests do not want to reach the server. get goes fine, but I change to post – nothing comes. Moreover, I make a request to my script (just a php script not tied to the engine)) – everything is going fine …

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

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

If I send a get-request, then the file is created in the root of the site. if I pass post, then even the file does not want to be created. It seems that it breaks off somewhere much earlier and even my code does not execute when sending a post request …

Answer:

Use:

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