我正在尝试将数据从角度提交到 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
.
为什么我得到空值。请帮忙!!