Ajax call controller in yii (javascript)

For some reason this does not work, and I cannot find a way to make it work!

on my controller named "ExplicacaoController" I have this:

public function accessRules() ... 'actions'=>array('index','view', 'test', 'ajaxrequest'), ... public function actionAjaxRequest() { $val1 = $_POST['val1']; $val2 = $_POST['val2']; echo "something"; Yii::app()->end(); } 

In my opinion, I:

 <script type="text/javascript"> ... $.ajax({ type: "POST", url: "<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?>", data: {val1:1,val2:2}, success: function(msg){ alert("Sucess") }, error: function(xhr){ alert("failure"+xhr.readyState+this.url) } }); ... 

What happens is that I always get this error:

 failure4<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?> 

I really need help with this

+4
source share
1 answer

Try to put

 <? echo Yii::app()->createUrl('Explicacao/ajaxRequest'); ?> 

instead

 <? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?> 

The fact is that in the creation URL you need to set the controller identifier, not the full name of the controller.

If it doesn’t work, you can try both Explicacao/ajaxRequest and Explicacao/ajaxRequest , because your route may be case sensitive depending on your conf

+6
source

Source: https://habr.com/ru/post/1447209/


All Articles