对Node.js(express.js)进行Ajax调用

对Node.js(express.js)进行Ajax调用,ajax,node.js,angularjs,Ajax,Node.js,Angularjs,我在尝试简单的事情。对node-express进行ajax调用,并在此基础上执行一些操作。我无法访问req.body,我的意思是从node.js端调试时它是空的 节点侧。 我正在使用: app.use(express.bodyParser()); 这是我在express中的测试方法: app.get('/courses', function(request, response) { console.log("I have been hit"); //I Am getting here

我在尝试简单的事情。对node-express进行ajax调用,并在此基础上执行一些操作。我无法访问req.body,我的意思是从node.js端调试时它是空的

节点侧。 我正在使用:

app.use(express.bodyParser());
这是我在express中的测试方法:

app.get('/courses', function(request, response) {
  console.log("I have been hit"); //I Am getting here               
});
角边:

eventsApp.factory('MainConnector', function($http, $q, $resource ) {
  return {
    mainConnection: function( ) {

      var requestBody = {
        id: 3,
        name: 'testname',
        lastname: 'testlastname'
      }

      $http.get("http://localhost:3000/courses", requestBody)
        .success(function(data, status, headers, config) {
          console.log("this is coming from wherever:" + data);
          $scope.data = data;
        }).error(function(data, status, headers, config) {
          $scope.status = status;
        });
    }      
  };
});
我正在尝试访问(从节点端)


但正文总是空的,就好像我没有发送任何东西一样。

您的ExpressJS测试处理程序实际上没有响应任何数据,这就是为什么返回空正文的原因。看看这本书

基本上你想要这样的东西

app.get('/courses', function(request, response) {
  console.log("I have been hit"); //I Am getting here
  response.send('hello world');            
});
其次,您正在尝试使用get请求发送数据。如果您查看angularjs文档,您将看到
$http.get
只接受一个参数,即url

这意味着您想要的AngularJS工厂更像这样:

eventsApp.factory('MainConnector', function($http, $q, $resource ) {
  return {
    mainConnection: function( ) 
      $http.get("http://localhost:3000/courses")
        .success(function(data) {
          console.log("this is coming from wherever:" + data);
        });
    }      
  };
});
但是假设您确实想要向服务器发送一些东西,您想要的是POST请求。这意味着您将更新express处理程序以响应POST而不是GET

app.get('/courses', function(request, response) {
  response.send(request.body);            
});
这是一个简单的“echo”处理程序。它只会将客户端发送给它的任何内容发送回客户端。如果你发送“你好”,它会回复“你好”

以及相应的AngularJS服务工厂

eventsApp.factory('MainConnector', function($http, $q, $resource ) {
  return {
    mainConnection: function( )
      var data = {name: "hello"};
      $http.post("http://localhost:3000/courses", data)
        .success(function(data) {
          console.log("this is coming from wherever:" + data);
        });
    }      
  };
});

事实证明,这是两个问题。是的,使用GET和发送有效负载时出现此问题。然而,这并没有解决问题。问题出在CORS。我就是这样修复的:

var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'content-type, Authorization, Content-Length, X-Requested-With, Origin, Accept');

    if ('OPTIONS' === req.method) {
        res.send(200);
    } else {
        next();
    }
};


req.body用于POST请求AFAIK,您在客户端执行get请求。Erik我接受了您的回答。谢谢。为了清楚起见-当您说更改get to a post时,也可以更新代码示例:app.post('/courses',function(request,response){response.send(request.body);});或者它不会像我刚刚发现的那样起作用——其他一切都很棒!
var allowCrossDomain = function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'content-type, Authorization, Content-Length, X-Requested-With, Origin, Accept');

    if ('OPTIONS' === req.method) {
        res.send(200);
    } else {
        next();
    }
};
app.configure(function () {
    app.set('port', process.env.PORT || 3000);
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(allowCrossDomain);
    app.use(app.router);   
});