2016-11-07 35 views
0

我试图使用邮递员yii2前端张贴,但我得到这个Yii2交法不允许

{ 
    "name": "Method Not Allowed", 
    "message": "Method Not Allowed. This url can only handle the following request methods: GET, HEAD.", 
    "code": 0, 
    "status": 405, 
    "type": "yii\\web\\MethodNotAllowedHttpException" 
} 

我不知道如何从前端张贴。

这是我的前端控制器代码

class TestController extends ActiveController { 
    public $modelClass = 'common\models\Test'; 

    public function behaviors(){ 
     $behaviors = parent::behaviors(); 
     $behaviors['corsFilter'] = [ 
      'class' => \yii\filters\Cors::className(), 
      'cors' => [ 
       'Origin' => ['*'], 
       'Access-Control-Expose-Headers' => ['X-Pagination-Current-Page', 'X-Pagination-Page-Count'], 
      ], 
     ]; 
     return $behaviors; 
    } 

    public function actions(){ 
     $actions = parent::actions(); 
     unset($actions['index']); 
     return $actions; 
    } 

    public function actionIndex(){ 
     $activeData = new ActiveDataProvider([ 
      'query' => Test::find()->orderBy('test_id DESC'), 
      'pagination' => [ 
       'defaultPageSize' => 5, 
      ] 
     ]); 
     return $activeData; 
    } 

} 

我是相当新的编程,需要对如何让POST方法一个非常简单的说明和示例。

http://localhost/advanced/frontend/web/index.php?r=test

回答

0

嗯,好像你继承\yii\rest\ActiveController控制器。它有verbs()方法,您必须在您的控制器中重写以提供适当的方法。下面是从原来的类verbs()

protected function verbs() 
{ 
    return [ 
     'index' => ['GET', 'HEAD'], 
     'view' => ['GET', 'HEAD'], 
     'create' => ['POST'], 
     'update' => ['PUT', 'PATCH'], 
     'delete' => ['DELETE'], 
    ]; 
} 

尝试在你写的TestController是这样的:

protected function verbs() { 
    $verbs = parent::verbs(); 
    $verbs['index'] => ['POST']; //methods you need in action 
    //or 
    $verbs['index'][] => 'POST'; //just add the 'POST' to "GET" and "HEAD" 
    return $verbs; 
}