AngularJS + ExpressJS。代理POST请求正在等待 [英] AngularJS + ExpressJS. Proxy POST request is pending

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

问题描述

 应用程序.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 浏览器。



这是我的发布方式:

  $ http {
method:'POST',
url:'/api.json',
data:$ .param({
title:not.title,
desc:not.description
}),//将数据作为字符串传递
标题:{'Content-Type':'application / x-www-form-urlencoded'}
))。success(function(){alert(Success);});

有什么问题?



编辑:
这是在控制台上看到的请求:

解决方案

你应该提到你正在使用请求库:



https:/ /github.com/mikeal/request



request.post()期待该窗体作为第二个参数:

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

或链接的电话:

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

因为你没有把它作为参数传递,request.form()根本没有提出任何请求,等待你打电话。形成()。但是,既然你也没有这样做,那么不会有任何请求发生,所以没有回复任何回复,因此你的应用程序看到请求失败而没有响应。您可以看到,在chrome开发者工具网络标签中,请求将显示(失败)状态代码。



所以只需从当前的表单数据获取请求并传递给request.form,它应该工作。



为了将来参考,调试器会告诉你这个错误是什么。我推荐Webstorm中包含的一个,但是可以随意使用任何调试器。



编辑:没有尝试,但这是我会尝试的

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


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

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

Here is how I 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");});

What's wrong?

EDIT: 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() is expecting the form either as the second parameter:

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

or as a chained call:

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

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.

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

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.

Edit: 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 + ExpressJS。代理POST请求正在等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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