角$ HTTP POST无法将数据发送到前pressjs [英] Angular $http POST unable to send data to expressjs

查看:144
本文介绍了角$ HTTP POST无法将数据发送到前pressjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从角度到前preSS发布的数据,但是,我eachtime使POST请求我得到这个错误。

 选项的http://本地主机/后/净:: ERR_CONNECTION_REFUSED(匿名函数)@ angular.js:11209s @ angular.js:11002克@ angular.js:10712(匿名函数)@ angular.js:15287米$ @的eval angular.js:。16554米$ @消化angular.js:16372米$ @适用angular.js:16662(匿名函数)@ angular.js:24283m.event.dispatch @的jQuery .js文件:// @ 4670r.handle的jquery.js:4338
controller.js:29

和errorCallback响应对象是:

 对象{数据:空,状态:-1,配置:对象,状态文本:}

我做了一些调整,在寻找上的其他类似的问题,如添加标题,内容类型等,但没有运气。

什么似乎是这里的问题。
谢谢

Controller.js

  word.add =功能(){
的console.log(word.name);    $ HTTP({
        方法:POST,
        网址:的http://本地主机/后/',
        数据:word.name,
        标题:{内容类型:应用/ JSON'}
    })。然后(函数successCallback(响应){
        的console.log(响应);
        的console.log(成功);
    },功能errorCallback(响应){        的console.log(响应);
        的console.log(失败);
    });
};

从相关code摘录 app.js

  VAR路线=要求('./路线/指数');
    app.all('/ *',函数(请求,响应下一个){
       response.header(访问控制允许原产地,*);
       response.header(访问控制允许的方法,GET,PUT,POST,DELETE');
        response.header(访问控制允许报头,X-请求-着,内容类型);
  下一个();
});app.use(记录器('开发'));
app.use(bodyParser.json());
app.use(bodyParser.urlen codeD({延长:假}));
app.use('/后,路线);

index.js

  VAR前preSS =要求('前preSS');
VAR路由器=前press.Router();
router.post('/后',函数(请求,响应){
    的console.log(request.body.name);
    response.send('后达成');
});module.exports =路由器;

编辑:
  我已经尝试过,并在我的回应添加的内容类型标题。
不过,这是同样的错误。

在同一code,而我用这个方法后正常工作。

  $ http.post('/后',{数据:word.name})。成功(功能(响应){
       的console.log(成功);
      })错误(功能(错误){
         的console.log(失败)
      });


解决方案

您必须在服务器上启用CORS。由于端口都是同一服务器上的不同,将像对待一个跨域请求。

http://enable-cors.org/server_ex$p$pssjs.html

I'm trying to post data from angular to express, however, eachtime I make the post request I get this error.

OPTIONS http://localhost/post/ net::ERR_CONNECTION_REFUSED(anonymous function) @ angular.js:11209s @ angular.js:11002g @ angular.js:10712(anonymous function) @ angular.js:15287m.$eval @ angular.js:16554m.$digest @ angular.js:16372m.$apply @ angular.js:16662(anonymous function) @ angular.js:24283m.event.dispatch @ jquery.js:4670r.handle @ jquery.js:4338
controller.js:29 

and the errorCallback response object is:

Object {data: null, status: -1, config: Object, statusText: ""} 

I did some tweaking looking at other similar questions on SO, like adding header, content-type etc. But no luck there.

What seems to be the problem here. Thanks

Controller.js

word.add = function(){
console.log(word.name );

    $http({
        method:'POST',
        url:'http://localhost/post/',
        data :word.name,
        headers: {'Content-Type': 'application/json'} 
    }).then(function successCallback(response){
        console.log(response);
        console.log("Successful");
    },function errorCallback(response){

        console.log(response);
        console.log("Unsuccessful");
    });     
};

Relevant code excerpt from app.js

 var routes = require('./routes/index');
    app.all('/*', function (request, response, next) {
       response.header("Access-Control-Allow-Origin", "*");
       response.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
        response.header("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
  next();
});

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/post', routes);

index.js

var express = require('express');
var router = express.Router();
router.post('/post', function(request, response){
    console.log(request.body.name);
    response.send('Reached post');
});

module.exports = router;

Edit: Hi, I've already tried and added the content-type header in my response. Still,It's the same error.

The same code works fine while I use this method to post.

$http.post('/post',{data: word.name}).success(function(response) {
       console.log("success");
      }).error(function(err){
         console.log("failure")
      });

解决方案

You must enable CORS on your server. Since the port is different on the same server it will treat it like a cross domain request.

http://enable-cors.org/server_expressjs.html

这篇关于角$ HTTP POST无法将数据发送到前pressjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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