簡體   English   中英

使Ajax調用Angular到Node.js(express.js)

[英]Making Ajax call Angular to Node.js (express.js)

我正在嘗試簡單的事情。 對node express進行ajax調用並根據它執行某些操作。 我無法訪問req.body,我的意思是當從node.js端調試時它是空的

NODE SIDE。 我在用:

app.use(express.bodyParser());

這是快遞中的礦山測試方法:

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

我正在嘗試訪問(從節點端)

req.body.name

但身體總是空的,好像我沒有發送任何東西。

你的ExpressJS測試處理程序實際上沒有響應任何數據,這就是為什么你得到一個空體。 查看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只接受1個參數,即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請求。 這意味着您更新快遞處理程序以響應POST而不是GET。

app.get('/courses', function(request, response) {
  response.send(request.body);            
});

這是一個簡單的“echo”處理程序。 無論客戶端發送給客戶端,它都會發送回客戶端。 如果你發送“Hello”,它將回復“你好”。

和相應的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();
    }
};

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM