Javascript Angular$http POST无法将数据发送到expressjs

Javascript Angular$http POST无法将数据发送到expressjs,javascript,angularjs,node.js,express,Javascript,Angularjs,Node.js,Express,我试图将数据从angular发布到express,但是,每次我发出post请求时,都会出现此错误 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:1655

我试图将数据从angular发布到express,但是,每次我发出post请求时,都会出现此错误

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 
errorCallback响应对象是:

Object {data: null, status: -1, config: Object, statusText: ""} 
我在其他类似的问题上做了一些调整,比如添加标题、内容类型等,但没有运气

这里似乎有什么问题。 谢谢

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");
    });     
};
 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);
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;
相关代码摘录自app.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");
    });     
};
 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);
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;
index.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");
    });     
};
 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);
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;
编辑: 您好,我已经尝试并在响应中添加了内容类型标题。 不过,这是同样的错误

当我使用此方法发布时,相同的代码工作正常

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

默认情况下,expressjs列表位于9000端口。所以,您发布的url应该是

,您必须在服务器上启用CORS。由于同一服务器上的端口不同,因此它会将其视为跨域请求


触发了飞行前可能的副本。这意味着我们已经准备好了。您可以使客户端和服务器位于同一域中,也可以添加CORS头。@Phil我已经研究了这个问题,添加了CORS,但仍然面临相同的错误。我不确定这是否与内容类型头错误有关。嗯,
net::ERR_CONNECTION_拒绝
让我觉得您的Express服务器实际上没有运行(或者至少没有在端口80上运行)。嗨,我遇到的一个奇怪行为是,我能够在使用此方法时成功发布数据。$http.post('/post',{data:word name})。成功(函数(响应){console.log(“成功”);}).error(函数(err){console.log(“失败”)});