【问题标题】:How to fix json parsing problems in Angular js如何修复 Angular js 中的 json 解析问题
【发布时间】:2016-01-28 19:47:26
【问题描述】:

我正在尝试使用 angular.js 通过http.post 将我的index.html 中的contactItem(字符串元素)发送到我的js 应用程序。 每次我尝试发布 contactItem 时都会出现以下错误:

SyntaxError: Unexpected token m (or D)

解析时。

我尝试了JSON.stringify(contactItem)JSON(contactItem);,但没有帮助。

希望有人能帮我解决这个问题。谢谢。

index.html

<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 
<table style="width:100%">
 <tr ng-repeat="contact in databases.databases">
    <td>{{ contact.name }} <button type="button"ng-click="addContactlist(contact.name)">Click Me</button></td>
    <td>{{ contact.sizeOnDisk }} </td>
    <td>{{ contact.empty }} </td>
  </tr>
  </table> 
</div>

</body>

global.js

var app = angular.module('myApp', []);

app.controller('customersCtrl', function($scope, $http) {
    console.log("controller connected");

function refresh(){ 
// create contacklist route 
$http.get('/databases').success(function(response) {
    console.log("recived data requested");
    $scope.databases = response; 
  });
}

// Call refresh to init cantacklist 
refresh(); 


// add Doc to table 
$scope.addContactlist = function(contactItem) {
  alert("Working ");
    $http.post('/collection', JSON.stringify(contactItem)).success(function(response) {
      });
    console.log("posted: "+contactItem);
  };


});// Controller 

mvc.js

var express = require('express');
var path = require('path'); //core module 
var databaseUrl = "localhost:27017/DB"; // default env
var bodyParser = require('body-parser');

var Db = require('mongodb').Db,
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    ReplSetServers = require('mongodb').ReplSetServers,
    ObjectID = require('mongodb').ObjectID,
    Binary = require('mongodb').Binary,
    GridStore = require('mongodb').GridStore,
    Grid = require('mongodb').Grid,
    Code = require('mongodb').Code,
    assert = require('assert');

//configure app
var app = express(); 
var db = new Db('DB', new Server('localhost', 27017));


db.on('error', function (err) {
    console.log('database error', err)
}); 

db.on('connect', function () {
    console.log('database connected')
}); 

// store all html files in views 
app.use(express.static(__dirname + '/views'));
// parses recived json input 
app.use(bodyParser.json());
// store all js in Scripts folder
app.use(express.static(__dirname + '/scripts'));

// Technology not needed but good practice, especailly when serval people are working on it 
app.get('/', function (req, res) {
    res.sendFile('index.html');
}); 

// listen for contactlist get request, aka transfers the contacklist in mongo to client
app.get('/databases', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {

      // Use the admin database for the operation
      var adminDb = db.admin();

      // List all the available databases
      adminDb.listDatabases(function(err, dbs) {
        assert.equal(null, err);
        assert.ok(dbs.databases.length > 0);
        console.log(dbs);
        res.json(dbs); 
        db.close();
      });
    });
}); 

// listen for contactlist get request, aka transfers the contacklist in mongo to client
app.get('/collection', function (req, res) {
    console.log("-- recived GET request --"); 
    db.open(function(err, db) {
         // Grab a collection without a callback no safe mode
        var col1 = db.collection('DB');
    });
}); 



// Implement a web server to listen to requests 
app.listen(4444, function(){
    console.log('ready on port 4444'); 
}); 

【问题讨论】:

    标签: javascript json angularjs node.js mongodb


    【解决方案1】:

    $http.post 需要数据应该以 json 格式传递,就像这里的 contactItem 只是 contact.name 不会在任何意义上受到攻击。我建议您在解析之前创建JSON 对象。

    $http.post('/collection', JSON.stringify({'contactItem': contactItem})
    //$http.post('url', data); //data should be in json format.
    

    同样在 node.js 代码上调用应该是 应用程序.post 而不是 app.get

    【讨论】:

    • 这修复了解析错误。但是 app.get() 并没有打印到控制台。我不应该使用 stringify() 吗?
    • 是的..请尝试不进行字符串化
    • SyntaxError: 解析时出现意外的令牌 D。是的,我知道还有什么可以尝试的
    • 是路由的问题吗?
    • 我认为 post call 应该是 app.post 而不是 app.get
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    • 2019-11-22
    • 2015-09-27
    • 1970-01-01
    • 2019-06-14
    • 2020-02-10
    • 2018-11-29
    相关资源
    最近更新 更多