将json数据从angularjs发送到nodejs
问题描述:
我在从前端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。这样我就可以解析并只将文本字段写入文件。
答
$ http POST调用,需要Object,而不是json。所以在发送给服务器之前,您需要对JSON进行字符串化。
$http.post('/sample',JSON.stringify($scope.jsondata)).success(function(data,status){
console.log("Success");
})
}
答
您是否将该帖子创建为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模块将其改回为对象
答
尝试设置数据类型为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
答
使用应用/ JSON而POST方法
$http({
method:'POST',
dataType: "json",
url: 'http://localhost:8000/saveform',
data : $scope.user,
headers: {
'Content-Type': 'application/json'
}
}).success(function(data){
});
您好,感谢的答复。我试过** JSON.stringify($ scope.jsondata)**。但是我仍然得到相同的输出。而在服务器端,我无法解析。 – naik3
任何其他方法都在那里,以便我们可以解析JSON并仅在angularjs一侧将文本字段写入文件。 – naik3