2016-12-14 110 views
0

我在從前端angularjs發送json數據到表達nodejs時遇到問題。這是我嘗試過的。將json數據從angularjs發送到nodejs

frontend.html

<form ng-submit="func()"> 
    <textarea name="inputtext" type="text" ng-model="sentence"></textarea> 
</form> 

backend.js

$scope.func = function(){ 

$scope.jsondata = {"status":"OK","language":"english","sentences":[{"sentence":"That's a nice restaurant."},{"sentence":"Also I went to another bad restaurant."},{"sentence":"I didn't like that movie."}]} 

$http.post('/sample',$scope.jsondata).success(function(data,status){ 
     console.log("Success"); 
    }) 
} 

server.js

var express = require('express'); 
var app = express(); 
var http = require('http').Server(app); 
var io = require('socket.io')(http); 
var bodyParser = require('body-parser'); 
var path = require('path'); 
var fs = require('fs'); 

app.set('views', __dirname + '/views'); 
app.set('view engine' , 'ejs'); 

app.use(bodyParser.json()); 
var urlencodedParser = bodyParser.urlencoded({ extended: true }); 

app.use(express.static('public')); 

app.get('/',function(req,res){ 
    res.render('index',{ title: 'Sentence' }); 
}); 
app.post('/sample',urlencodedParser,function(req,res){ 
    console.log(req.body); 
}); 
http.listen(8888, function(){ 
    console.log("Server listening on 8888"); 
}); 

我我沒有在節點服務器部分獲得確切的JSON。這是我得到的。

輸出

{ '{"status":"OK","language":"english","sentences":': { '{"sentence":"That\'s a nice restaurant."},{"sentence":"Also I went to another bad restaurant."},{"sentence":"I didn\'t like that movie."},{"sentence":"Thats a very bad movie."}': '' } } 

任何一個可以幫助我如何才能獲得在該節點服務器部分精確JSON。這樣我就可以解析並只將文本字段寫入文件。

回答

0

$ http POST調用,需要Object,而不是json。所以在發送給服務器之前,您需要對JSON進行字符串化。

$http.post('/sample',JSON.stringify($scope.jsondata)).success(function(data,status){ 
     console.log("Success"); 
    }) 
} 
+0

您好,感謝的答覆。我試過** JSON.stringify($ scope.jsondata)**。但是我仍然得到相同的輸出。而在服務器端,我無法解析。 – naik3

+0

任何其他方法都在那裏,以便我們可以解析JSON並僅在angularjs一側將文本字段寫入文件。 – naik3

0

您是否將該帖子創建爲JSON以發送至節點?

Angular會爲你做這件事,你可以在帖子中提出並反對,因爲Ved建議Angular會奇蹟般地將其改爲JSON發送給節點服務器。

$scope.func = function(){ 

$scope.data = {language:"english", sentences: [{sentence:"That's a nice restaurant."},{sentence:"Also I went to another bad restaurant."}, {"sentence":"I didn't like that movie."}]} 

$http.post('/sample',$scope.data).success(function(data,status){ 
    console.log("Success"); 
}) 
} 

這將讀取服務器上:[Object object]作爲節點的bodyparser NPM模塊將其改回爲對象

0

嘗試設置數據類型爲JSON之前發送到服務器。這將設置Content-Type頭的application/json該服務器可以理解爲一個JSON

$http({ 
    method: 'POST', 
    url: baseUrl + '/sample', 
    dataType: "json", 
    data: { 
     "status": "OK", 
     "language": "english", 
     "sentences": [{"sentence": "That's a nice restaurant."}, 
      {"sentence": "Also I went to another bad restaurant."}, 
      {"sentence": "I didn't like that movie."}] 
    } 
}).success(function (data, status) { 

}).error(function (data, status) { 

}) 

更新: 我試圖運行你的源代碼並修改一點點,你可以檢查它是否響應正確的格式,你想要的。結帳這個回購:在頭https://github.com/nguyennb9/sample-angularjs-expressjs

+0

嗨,謝謝你的回覆..輸出沒有變化。它仍然採用這種格式:'{'{「status」:「OK」,「language」:「english」,「sentences」:':{'{「sentence」:「那是一家不錯的餐廳。」 },{「sentence」:「我也去了另一家不好的餐廳。」},{「句子」:「我不喜歡那部電影。」},{「句子」:「這是一部非常糟糕的電影。 }':''}} ' – naik3

+0

你能檢查我的答案的更新。 – Finn

0

使用應用/ JSON而POST方法

$http({ 
    method:'POST', 
    dataType: "json", 
    url: 'http://localhost:8000/saveform', 
    data : $scope.user, 
    headers: { 
     'Content-Type': 'application/json' 
    } 
    }).success(function(data){ 

    }); 
相關問題