【发布时间】:2016-02-12 04:22:13
【问题描述】:
客户端有表单和按钮,我想将用户在表单中键入的数据发送到服务器,服务器有将数据保存到数据库的请求处理程序,并从数据库发送到客户端。
我该怎么做我对逻辑感到困惑,我认为使用了正文解析器,还有标题的作用,在这种情况下请求选项,我找到了解决方案,但我没有盲目实施,我只是想在了解后按照自己的方式去做
在客户端:
let headers: Headers = new Headers();
headers.append('Content-Type', 'application/json');
let opts: RequestOptions = new RequestOptions();
opts.headers = headers;
this.http.post(
'http://localhost:3000/addStudent',
JSON.stringify(obj),
opts
).subscribe((res: Response) => {
console.log(res.json())
setTimeout(() => {
this.students = res.json();
}, 3000)
})
在服务器端:
app.post('/addStudent',function(req,res) {
var newStudent = new StudentModel(req.body);
console.log(req.body);
newStudent.save();
StudentModel.find(function(err,data) {
if(err) res.send(err)
else res.json(data)
})
【问题讨论】:
标签: angular angular2-http