从angularjs向nodejs发送JSON数据

时间:2016-12-14 18:26:08

标签: javascript angularjs json node.js express

我在从前端angularjs发送json数据以表达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。这样我就可以解析并只将句子字段写入文件中。

4 个答案:

答案 0 :(得分:0)

$ http POST调用,使用Object,而不是json。所以你需要在将JSON发送到服务器之前对其进行字符串化。

XLApp.Calculation = XLApp.xlCalculationManual 

答案 1 :(得分: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模块会将其更改回Object

答案 2 :(得分: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

答案 3 :(得分:0)

POST方法

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

  });