角4点POST
角4.4.4
的HttpClient无法发送的数据这是我的应用程序组件角4点POST
constructor(
private http: HttpClient,
)
this.http.post('/api.php', {name, age}).subscribe(data => {
console.log(data);
});
api.php -> exit(json_encode($_POST));
没有收到$ _ POST
返回任何数据[] ;
(let xmlRequest = new XMLHttpRequest();
.... 正常工作)
我尝试设置头
let headers = new HttpHeaders().set('Content-Type', 'application/json; charset=UTF-8');
不行
对不起,这个问题,但我花了1天,还是没找到解。
ps。客户端和服务器有相同的来源。
我找到了解决这个。
在PHP中,$_POST
只接受formdata。
随着请求头 '内容类型:应用程序/ JSON' 你可以用file_get_contents('php://input');
所以
$_POST = json_decode(file_get_contents('php://input'));
请尝试,我希望它会帮助你
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import 'rxjs/add/operator/map';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class LandingService {
private apiUrl = 'http://localhost:5000/';
list:any;
headers : any;
constructor(private _http: HttpClient){
this.headers = new Headers();
this.headers.append('Content-Type', 'application/json');
}
getsearchResponse(searchText){
this.list ={"sentences":searchText}
return this._http.post(this.apiUrl+'searchBotsNew',this.list,this.headers)
.map(res =>res.json())
.do(data => console.log(JSON.stringify(data)));
}
}
谢谢。但我想尝试新的HttpClient –
有你在app.module.ts文件中导入HttpClientModule? –
是的,我做到了。请求发送但服务器不recive数据 –
收到请发表你的PHP方法 –
出口(json_encode($ _ POST)) –
我的意思,您的签名,您的方法完全 –