Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/452.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Angular4不工作向yii2申请发送post请求_Javascript_Ajax_Angular_Http_Yii2 - Fatal编程技术网

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之前的错误相同。或更改内容类型。