yii 中的 ajax 调用控制器(javascript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13473577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ajax call controller in yii (javascript)
提问by user1772093
For some reason this doesnt work and I can't find a way to make it work!
出于某种原因,这不起作用,我找不到让它起作用的方法!
on my controller named "ExplicacaoController" I have this:
在我名为“ExplicacaoController”的控制器上,我有这个:
public function accessRules()
...
'actions'=>array('index','view', 'test', 'ajaxrequest'),
...
public function actionAjaxRequest()
{
$val1 = $_POST['val1'];
$val2 = $_POST['val2'];
echo "something";
Yii::app()->end();
}
On my view I have:
在我看来,我有:
<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 allways get this error:
发生的情况是我总是收到此错误:
failure4<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?>
I really need some help with this
我真的需要一些帮助
回答by darkheir
Try to put
试着放
<? echo Yii::app()->createUrl('Explicacao/ajaxRequest'); ?>
instead of
代替
<? echo Yii::app()->createUrl('explicacaoController/ajaxRequest'); ?>
The thing is in create url you need to put the Controller ID not the Controller full name.
事情是在创建 url 中,您需要输入控制器 ID 而不是控制器全名。
If it's not working you could try both Explicacao/ajaxRequest
or explicacao/ajaxRequest
because your rout could be case sensitive depending on your conf
如果它不起作用,您可以同时尝试,Explicacao/ajaxRequest
或者explicacao/ajaxRequest
因为您的路由可能区分大小写,具体取决于您的配置
回答by King Art
try this
试试这个
url: $(location).attr('pathname') + '?r=anyControllerId/actionId',
This will get the path name of the current URL
这将获得当前 URL 的路径名