我在從前端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。這樣我就可以解析並只將文本字段寫入文件。
您好,感謝的答覆。我試過** JSON.stringify($ scope.jsondata)**。但是我仍然得到相同的輸出。而在服務器端,我無法解析。 – naik3
任何其他方法都在那裏,以便我們可以解析JSON並僅在angularjs一側將文本字段寫入文件。 – naik3