我试图从角度2向node.js发送post请求。出于某种原因,在提交表单后,我在浏览器的url栏中看到结果,就像发送get请求一样。此外,它正在重新加载页面。我没有从角度和节点中得到任何控制台错误。我和邮递员查过了,服务器工作正常。
在发送请求之后,url看起来是这样的;
http://localhost:3000/?username=testUser&email=test%test.com&password=34242
//server.js
app.post('/register', function(req, res){
res.send('hello');
console.log('success')
})
//http.service
sendMyData(users : any) {
let bodyString = JSON.stringify(users); // Stringify payload
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
return this.http.post('http://localhost:3000/register', bodyString, options)
.map((res : Response) => res.json())
}
//header.component
onSubmit(form : NgForm) {
this.httpService.sendMyData(form)
.subscribe(
(data) => alert(data)
)
}
//header.component.html
<form (ngSubmit)='onSubmit(f)' #f='ngForm'>
.......
</form>发布于 2017-05-07 16:09:42
我刚刚用this.httpService.sendMyData(form.value)解决了这个问题,我完全忘记了"form“用各种属性返回整个对象。所以必须是form.value
https://stackoverflow.com/questions/43826124
复制相似问题