Angular post方法的响应为空
我正在开发一个连接到服务器的ionic应用程序。我正在从应用程序调用post方法。我收到的是空的JSON文件。但是当我试着从邮递员那里得到预期的回应Angular post方法的响应为空,angular,ionic3,Angular,Ionic3,我正在开发一个连接到服务器的ionic应用程序。我正在从应用程序调用post方法。我收到的是空的JSON文件。但是当我试着从邮递员那里得到预期的回应 addLocation(coordinates) { console.log(coordinates) return new Promise((resolve, reject) => { this.http.post(this.localurl + '/location', JSON.stringify(coordinates), {
addLocation(coordinates) {
console.log(coordinates)
return new Promise((resolve, reject) => {
this.http.post(this.localurl + '/location', JSON.stringify(coordinates), {
headers: new HttpHeaders().set('Content-Type', 'application/json'),
})
.subscribe(res => {
resolve(res);
}, (err) => {
console.log("rejected");
reject(err);
});
});
}
通过这个方法,我将用户的位置发送到服务器。然后它必须接收附近的商店。但我收到的是空的JSON文件。但是我从邮递员那里得到了预期的回应。使用
toPromise
将可观察到的转化为承诺
addLocation(coordinates) {
console.log(coordinates)
return this.http.post(this.localurl + '/location', JSON.stringify(coordinates), {
headers: new HttpHeaders().set('Content-Type', 'application/json'),
})
.toPromise()
});
现在从组件调用它
yourService.addLocation(coordinates).then((res => {
console.log(res)
}, (err) => {
console.log("rejected");
})
使用
toPromise
将可观察到的内容转换为承诺
addLocation(coordinates) {
console.log(coordinates)
return this.http.post(this.localurl + '/location', JSON.stringify(coordinates), {
headers: new HttpHeaders().set('Content-Type', 'application/json'),
})
.toPromise()
});
现在从组件调用它
yourService.addLocation(coordinates).then((res => {
console.log(res)
}, (err) => {
console.log("rejected");
})
我使用了toPromise方法。但同样的结果“length:0\uuuu proto\uuuu:Array(0)”我使用了toPromise方法。但是相同的结果是“长度:0__;proto__;:数组(0)”