Ajax调用不起作用,在Yii Framework中显示“404 not found error”

时间:2016-09-08 11:51:34

标签: php ajax yii

我在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)

1 个答案:

答案 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';   
  }