我试图发布" uid" ,但服务器显示" undefined" ,我没有'我知道为什么会这样!即使我正确设置了标题,它仍然无效,请帮助我。
这里的应用代码:
HTML
<form (ngSubmit)="onSubmit()">
User Id: <input type="text" name="uid" [(ngModel)] = 'uid'> <br>
<input type="submit" value="Submit">
</form>
onSubmit 功能
onSubmit(value) {
var headers = new Headers();
var data = { uid: this.uid };
headers.append('Content-Type', 'application/json');
this.http.post('http://192.168.50.193:1000/process_post', JSON.stringify(data), {headers: headers})
.subscribe((data)=>
console.log("POST::: "+data);
);
}
server.js (明确)发布方法:
app.post('process_post', urlencodedParser, function (req, res) {
response = {
first_name:req.body.uid
};
console.log(req.body.uid);
res.end(JSON.stringify(response));
})
&#13;
点击提交命令提示后,我得到&#39; undefined&#39; ,为什么?
答案 0 :(得分:0)
尝试map
方法:
onSubmit(value) {
var headers = new Headers();
var data = { uid: this.uid };
headers.append('Content-Type', 'application/json');
this.http.post('http://192.168.50.193:1000/process_post', JSON.stringify(data), {headers: headers})
.map(data => data.json())
.subscribe((data)=>
console.log("POST::: "+data);
);
}