Javascript Angular4不工作向yii2申请发送post请求
我使用这个代码Javascript Angular4不工作向yii2申请发送post请求,javascript,ajax,angular,http,yii2,Javascript,Ajax,Angular,Http,Yii2,我使用这个代码 this.http.post('http://l.example/angular/create/', {name: 'test'}).subscribe( (response) => console.log(response), (error) => console.log(error) ); 将post请求发送到我的Yii2 rest应用程序,但我在chrome控制台上遇到此错误,但我使用jquery$。post请求工作正常 我的
this.http.post('http://l.example/angular/create/', {name: 'test'}).subscribe(
(response) => console.log(response),
(error) => console.log(error)
);
将post请求发送到我的Yii2 rest应用程序,但我在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;
}
}
注意:
- Angular 4
正常工作并列出所有记录http.get
- 我使用yii2.0.14
- 角度4.3.1
我的错误在哪里?Yii中预定义的RESTful端点没有前导斜杠 应该是:
'http://l.example/angular/create'
而不是
'http://l.example/angular/create/'
Yii中预定义的RESTful端点没有前导斜杠 应该是:
'http://l.example/angular/create'
而不是
'http://l.example/angular/create/'
http.post
采用第三个参数options
。这包括标题。您可能必须使用此const header=new Headers({'Content-Type':'application/json','Accept':'application/json})将其传入。@ecain I;const options=newrequestoptions({headers:header})代码>但不工作错误是什么?与尝试取出accept之前的错误相同。或者更改内容类型。http.post
采用第三个参数options
。这包括标题。您可能必须使用此const header=new Headers({'Content-Type':'application/json','Accept':'application/json})将其传入。@ecain I;const options=newrequestoptions({headers:header})代码>但不工作错误是什么?与尝试取出accept之前的错误相同。或更改内容类型。