AngularJS + ExpressJS。 代理POST请求正在等待
使用AngularJS + Express我有以下代码将我的请求代理到远程服务:
app.get('/api.json', function (req, res) { req.pipe(request("http://test-api.com/api.json")).pipe(res); }); app.post('/api.json', function (req, res) { req.pipe(request.post("http://test-api.com/api.json")).pipe(res); });
所有的GET请求工作正常,但POST请求在我的浏览器中挂起。
以下是我的post:
$http({ method: 'POST', url: '/api.json', data: $.param({ "title": not.title, "desc": not.description }), // pass in data as strings headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function () {alert("Success");});
怎么了?
编辑:这是在控制台上看到的请求:
我应该检查什么来提供更多的信息?
你应该提到你正在使用请求库:
https://github.com/mikeal/request
request.post()期待的forms作为第二个参数:
request.post('http://service.com/upload', {form:{key:'value'}})
或者作为一个链接的电话:
request.post('http://service.com/upload').form({key:'value'})
因为你没有把它作为parameter passing,所以request.form()根本没有发出任何请求,等待你调用.form()。 但是既然你没有这样做, 就不会有任何请求发生,所以没有回应,所以你的应用程序看到请求失败而没有响应 。 您可以看到,在Chrome开发人员工具networking选项卡中,请求将显示“(失败)”状态码。
所以只需从当前请求中获取表单数据并将其传递给request.form即可。
为了以后的参考,debugging人员会告诉你这个错误是什么时候发生的。 我推荐Webstorm中包含的一个,但随意使用任何debugging器。
编辑:没有尝试,但这是我会尝试
app.post('/api.json', function (req, res) { req.pipe(request.post("http://test-api.com/api.json", {form:req.body})).pipe(res); });