这是我的http代码:
let headers = new Headers ({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
if (body == null)
{
body = {title: "hi", search: "person"};
}
console.log('body: ' + JSON.stringify(body));
return this.http.post('app/php/check.php', body, options)
.toPromise()
.then(response => response.text())
.catch(this.handleError);我不确定服务器是否正确地接收了数据,但我确信它发布了一些内容,正如我所看到的:
[
{
"select": "title",
"search": "person"
}
]带着火虫。
在我的php文件中,我所做的只是print_r($_REQUEST),但是它返回的只是一个空数组。
我做错什么了吗?
额外的问题:除了.json()和.text()之外,还有其他数据提取功能用于响应吗?
更新:
有趣的是,如果我使用url app/php/check.php?fruit=apple,就会有一个响应。有人知道这个差异吗?
发布于 2016-12-14 01:59:16
服务器应该正在接收您的body变量中的数据。
下面是一个如何从函数中检索输出的示例。由于post数据使用的测试API,示例使用了JSON输出。字符串将是相同的,只需更改返回部分。
ngOnInit() {
this.postRquest().then(results => this.response = results);
}
postRquest(body) {
let headers = new Headers ({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
if (body == null)
{
body = {title: "hi", search: "person"};
}
console.log('body: ' + JSON.stringify(body));
return this.http.post('app/php/check.php', body, options)
.toPromise()
.then((response) => return response.json())
.catch(error => {
console.log( error );
});
}确保您的内容类型标头为application/x-www-form-urlencoded
https://stackoverflow.com/questions/41133241
复制相似问题