express无法接收到angular4中http.post过来的参数
express无法接收到angular4中http.post过来的参数,试了好多方法都不管用,req中始终拿不到参数。代码如下:
var express = require('express');
var request = require('request');
var bodyParser = require('body-parser');
var multer = require('multer');
var upload = multer({ dest: 'uploads/' })
var app = express();
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-
app.get('/', (request, response) => response.send('这里是首页'));
app.post('/api/login', (req, resq) => {
console.log(req)
var userName = "";
var password= "";
request('http://localhost:8080/login?code='+userName+"&password=1", {form:{'code': userName, 'password':password}}, function (error, response, body) {
console.log(body)
resq.send(body);
resq.end();
});
});
app.get("*", (request, response) => response.end("404!"));
const server = app.listen(3000, 'localhost', () => {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
angular的代码如下:
login(info: any) {
var headers = new HttpHeaders();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let params = new HttpParams();
params.set('userName', info.userName)
params.set('password', info.password)
this.httpClient.post('http://localhost:3000/api/login',params).subscribe(
(val) => {
console.log("POST call successful value returned in body",
val);
},
response => {
console.log("POST call in error", response);
},
() => {
console.log("The POST observable is now completed.");
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的http的方法使用错了,那个方法是
你可以这么写,reques是一个Object: