2017-01-19 6 views
-1

ノードをnodejsルートに送信しようとしています。私はエクスプレスルートを使用しています。ノードからノードへのデータを投稿するjs route not working

var app = angular.module("myapp", []) 
app.controller("ListController2", ['$scope','getAverage','$http',function($scope, getAverage,$http) { 
$scope.subjectAverages = [{ 
'subjectName': '', 
'yearSem': '', 
'studentsAppeared': '', 
'studentsPassed':'', 
'percentage':'', 
'average':'', 
'facultyComment':'' 
}]; 


$scope.save=function(){ 

var data =angular.toJson($scope.subjectAverages) 
var objectToSerialize={'object':data}; 
$http({ 
    url: 'app', 
    method: "POST", 
    data: $.param(objectToSerialize), 
    headers: { 
      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' 
    } 
}).success(function(data){ 
    alert("done"); 
}); 

} 
router.post('/app',function(res,req){ 
    console.log(req.body); 
    }); 

ご案内してください。

+0

あなたのノードのapp.jsに急行-bodyparserモジュールを使用してい –

+0

はい私はそれを使用しています。 – dpak

+0

$ .paramを使用してデータを直列化しないでください。 POSTリクエストでparamsを送ることはできません。あなたのHTTPリクエストのデータ部分に、{object:$ scope.subjectAverages} –

答えて

0

はこれを試してみてください:

$scope.save=function(){ 

var data = {object : $scope.subjectAverages}; 
$http({ 
    url: '/app', 
    method: "POST", 
    data: data, 
    headers: { 
      'Content-Type': 'application/json' 
    } 
}).then(function(data){ //.success is deprecated,so use .then 
    alert("done"); 
}) 
    .catch(function(err){//using .catch instead of .error as it is deprecated 
    console.log("Error in request =>", err) 
}); 
+0

コンソール上でその定義が未定義です。 – dpak

+0

@Sajeetharan broはこれを手伝ってくれます。 – dpak

+0

どのようにバックエンドでボディーパーサーを使用していますか? –

関連する問題