0

I am trying to submit data from angular to Yii2

This is my service in angular where I am trying to send data through 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);
      }
    }

Below is my actionSubscribe in yii2. The API which is getting hit in angular

    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; 
    }

When I call the API and try to print the $_POST, I am getting the result as null.

Why am I getting the null value. Please help!!

rji rji
  • 697
  • 3
  • 17
  • 37
  • Where do you try to print the $_POST? Did you already check your network tab in the inspector? are you sure the data in the body is not empty? – Napinator May 15 '19 at 12:32
  • you should first try to narrow down the problem, and identify if the js is the culprit or backend server, and rule out the js for a moment and send a request via postman and see if you still get null ? – Muhammad Omer Aslam Aug 30 '19 at 23:41

0 Answers0