【发布时间】:2016-11-06 12:44:44
【问题描述】:
我的 nodejs 实例中有代码,它接收到一个发布请求(成功)并且还显示了通过 json 发送的参数(我在服务器端需要 body-parser)。收到帖子请求后,我立即执行return "testing"; 以检查该值是否已成功返回。但是,我的 angular2 回调(如图所示完成)不会触发或显示任何日志。知道为什么吗?
var to_send = JSON.stringify({"test": argument});
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http
.post('http://localhost:8080/',
to_send, {
headers: headers
})
.map((res) => res.json() )
.subscribe(
(response) => { console.log("Success Response" + response)},
(error) => { console.log("Error happened" + error)},
() => { this.parseResponse(res); }
);
parseResponse 函数只是 console.logs("something returned");
编辑:
这是我的代码现在的样子,但仍然失败(没有触发 parseResponse 中的日志):
var to_send = JSON.stringify({"test": argument});
var headers = new Headers();
headers.append('Content-Type', 'application/json');
this.http
.post('http://localhost:8080/',
to_send, {
headers: headers
})
.map((res) => res.json() )
.subscribe(
(response) => { console.log("Success Response",response)},
(error) => { console.log("Error happened",error)},
() => { this.parseResponse(res); }
);
在我的服务器中,我返回以下内容:
var to_return = {};
to_return["message"] = "success";
return to_return;
不过,它根本不起作用。知道为什么吗? parseResponse 是一个简单的日志“收到反馈”...
【问题讨论】: