首页 > 解决方案 > Angular 7 http.post 正在向 yii2 API 发送 null

问题描述

我正在尝试将数据从角度提交到 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);
      }
    }

下面是我actionSubscribe在yii2中的。受到角度影响的 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.

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

标签: angularyii2angular7angular-httpyii2-api

解决方案


推荐阅读