我使用此代码
this.http.post('http://l.example/angular/create/', {name: 'test'}).subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
将帖子请求发送到我的Yii2休息应用程序,但我在Chrome控制台上收到此错误,但是当我使用jquery $.post
请求正常工作时
我的Yii2控制器代码
namespace app\controllers;
use yii\rest\ActiveController;
class AngularController extends ActiveController
{
public $modelClass = 'app\models\Angular';
public function beforeAction($action)
{
$this->enableCsrfValidation = false;
return parent::beforeAction($action);
}
public function behaviors()
{
$behaviors = parent::behaviors();
// add CORS filter
$behaviors['corsFilter'] = [
'class' => \yii\filters\Cors::className(),
'cors' => [
'Origin' => ['*'],
'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'],
'Access-Control-Request-Headers' => ['*'],
],
];
$behaviors['contentNegotiator'] = [
'class' => \yii\filters\ContentNegotiator::className(),
'formats' => [
'application/json' => \yii\web\Response::FORMAT_JSON,
],
];
return $behaviors;
}
}
注意:
http.get
正常工作并列出所有记录我的错误在哪里?
答案 0 :(得分:0)
Yii中的预定义RESTful端点没有前导斜杠。
应该是:
'http://l.example/angular/create'
而不是
'http://l.example/angular/create/'