Angular 7 http.post向yii2 API发送null

时间:2019-05-15 07:49:13

标签: angular yii2 angular7 angular-http yii2-api

我正在尝试将数据从angular提交到Yii2

这是我提供的角度服务,我试图通过POST发送数据

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpHeaders } from '@angular/common/http'

    @Injectable({
      providedIn: 'root'
    })
    export class SubscribeService {
      readonly postURL = "http://localhost/sampleproject/frontend/web/api/subscribe";
      constructor(private http:HttpClient) { }

      subscribeSubmit(subscriber){
        let bodyString = JSON.stringify(subscriber); 
        let headers = new HttpHeaders({
          'Content-Type': 'application/json' });
      let options = { headers: headers };
        return this.http.post(this.postURL, bodyString, options);
      }
    }

以下是我在yii2中的actionSubscribe。该API遇到了麻烦

    public function actionSubscribe(){

        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: POST");
        header("Access-Control-Allow-Headers: Origin, Methods, Content-Type");
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
        return $_POST; 
    }

当我调用API并尝试打印$ _POST时,得到的结果为null

为什么我得到空值。请帮忙!

1 个答案:

答案 0 :(得分:0)

您在哪里尝试打印$ _POST?您是否已经在检查器中检查了“网络”选项卡?您确定正文中的数据不为空吗?