AngularJS +防爆pressJS。代理POST请求未决 [英] AngularJS + ExpressJS. Proxy POST request is pending

查看:135
本文介绍了AngularJS +防爆pressJS。代理POST请求未决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用AngularJS +防爆preSS
我有以下的code来代理我的请求到远程服务:

Using AngularJS + Express I have the following code to proxy my requests to a remote service:

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请求工作正常,但是发表挂起的请求我的浏览器。

All GET requests works fine, however the POST requests are pending in my browser.

下面是如何我张贴:

$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");});

怎么了?

编辑:
这是因为看到控制台上的要求:

Here is the request as seen on the console:

我应该怎么检查,以提供更多的信息?

What should I check to give more information?

推荐答案

您正在使用要求图书馆您应该提到:

You should have mentioned you were using the request library:

https://github.com/mikeal/request

request.post()期望的形式,无论是作为第二个参数:

request.post() is expecting the form either as the second parameter:

request.post('http://service.com/upload', {form:{key:'value'}})

或链式调用:

request.post('http://service.com/upload').form({key:'value'})

由于你没有把它当作一个参数,的request.form()不作出任何要求不惜一切,等着你来调用.FORM()。但是因为你不这样做,要么,否要求不断发生,所以永远没有返回答案,因此您的应用程序可以看到该请求失败,没有响应。你可以看到,在Chrome开发者工具的网络选项卡,在该请求将呈现(失败)状态code。

Because you're not passing it as an argument, request.form() is not making any request at all, waiting for you to call .form(). But since you're not doing that either, no request ever happens, so no answer is ever returned, and thus your application sees that the request failed without response. You can see that in the chrome developer tools network tab, where the request will show a "(failed)" status code.

所以才获得当前请求表单数据,并把它传递给REQUEST.FORM,它应该工作。

So just obtain the form data from the current request and pass it to request.form and it should work.

有关将来参考,调试器会告诉你的错误是什么瞬间。我建议包括Webstorm之一,但随意使用任何调试器都没有。

For future reference, a debugger would have told you what the mistake was instantly. I recommend the one included with Webstorm, but feel free to use any debugger at all.

编辑:没有尝试过,但是这是我想尝试

Haven't tried but this is what I would try

app.post('/api.json', function (req, res) {
    req.pipe(request.post("http://test-api.com/api.json", {form:req.body})).pipe(res);
});

这篇关于AngularJS +防爆pressJS。代理POST请求未决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆