我在yii框架中创建了简单的ajax调用函数,但是它没有工作,它在控制台中给出了404 not found错误消息。
main.php页面中的Ajax函数
$.ajax({
url: 'actionSendSms',
type: 'GET',
data: {user_id :array_id[i]},
success: function(response){
console.log(response);
}
});
控制器中的“actionSendSms”:
public function actionSendSms(){
return '1';
}
控制器中的 accessRules()函数:
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','actionSendSms'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','actionSendSms'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete','actionSendSms'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
来自控制台的错误消息是:
GET http://www.domain.com/admin/index.php/sendSms/actionSendSms?user_id=32 404 (Not Found)
答案 0 :(得分:1)
您应该使用不带“action”前缀的呼叫
$.ajax({
url: 'sendSms',
type: 'GET',
data: {user_id :array_id[i]},
success: function(response){
console.log(response);
}
});
控制器中的 accessRules()函数:
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view','sendSms'),
'users'=>array('*'),
),
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions'=>array('create','update','sendSms'),
'users'=>array('@'),
),
array('allow', // allow admin user to perform 'admin' and 'delete' actions
'actions'=>array('admin','delete','sendSms'),
'users'=>array('admin'),
),
array('deny', // deny all users
'users'=>array('*'),
),
);
}
尝试echo for response
public function actionSendSms(){
echo '1';
}