老师可否弄一个post 的 例子 这个服务器都是get的示例 一般项目中也都是post请求居多
来源:8-2 web服务器

慕田峪2064799
2017-06-11
1回答
-
慕田峪2064799
提问者
2017-06-11
我自己参照其他例子弄了一个,可以发送和接受json 但是 在server端不能解析body的数据 先贴上代码吧
这是angular端的post代码:
constructor(private http: Http) {
const headers = new Headers();
headers.append('Content-Type', 'application/json;charset=utf-8');
const creds = {
username: 'admin',
password: '123'
}
this.http.post('/api/addstock',creds,{headers:headers}).subscribe(
(res:Response) => console.log(res.json())
);
}
这是node server端的代码:
var express = require('express');
var bodyParser = require('body-parser');
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: true}));
// parse application/json
app.use(bodyParser.json())
app.post('/api/addstock', function(req, res){
var data = req.params;
console.log(data.username);
res.json({name: 'p2p'});
});
const server = app.listen(8000, "localhost", () => {
console.log(" 服务器已启动, 地址是:http://localhost:8000");
});
012017-06-11
相似问题