【问题标题】:"SyntaxError: Unexpected token n" in Express Node ServerExpress 节点服务器中的“SyntaxError: Unexpected token n”
【发布时间】:2015-12-29 09:19:20
【问题描述】:

我收到此错误-

SyntaxError: Unexpected token n
    at parse (/Users/afroza/node_modules/body-parser/lib/types/json.js:83:15)
    at /Users/afroza/node_modules/body-parser/lib/read.js:116:18
    at invokeCallback (/Users/afroza/node_modules/raw-body/index.js:262:16)
    at done (/Users/afroza/node_modules/raw-body/index.js:251:7)
    at IncomingMessage.onEnd (/Users/afroza/node_modules/raw-body/index.js:308:7)
    at IncomingMessage.emit (events.js:104:17)
    at _stream_readable.js:908:16
    at process._tickCallback (node.js:355:11)

我用nodejs创建路由报这个错误,试了很多都没办法,总是出现这个错误,

我在 express 框架中的服务器设置-

var express = require('express');

var app = express()
    , fs = require('fs')
    , path = require('path');

var env = process.env.NODE_ENV || 'production'
    , config = require('./config/config')[env]
    , mongoose = require('mongoose');

var http = require('http');

var db = mongoose.connect('mongodb://localhost/orbitax');


app.use(express.static(__dirname + '/public'));


var bodyParser = require('body-parser'); // add body-parser for interact with server and client data


var jsonParser = bodyParser.json(); //join this body parser with json file

var urlencodedParser = bodyParser.urlencoded({ extended: false});


//Initialize Models
var models_path = __dirname + '/app/models'
fs.readdirSync(models_path).forEach(function (file) {

  require(models_path+'/'+file)
});

require('./config/routes')(app,config);

var server = app.listen(9000, function(){
    var host = server.address().address;
    var port = server.address().port;

});

服务器路由,

var async = require('async')
    , mongoose = require('mongoose')
    , uploadModel = mongoose.model('uploadModel');


module.exports = function (app) {


  // upload routes

  var upload = require('../app/controller/upload');

  app.get('/newUpload', upload.newUpload);

  app.post('/create_new_directory', upload.create_new_directory);


}



exports.create_new_directory = function(req, res)
{
    console.log("hello")
    console.log(req.body);
}

来自客户端的请求是,

$http({'method': 'post', 'url': '/create_new_directory', data: $scope.directory })
            .success(function(data){
                console.log(data)
            })
            .error(function(){

            })

任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 在您的 Angular 应用程序中进行此 $http 调用时,$scope.directory 的值是多少?
  • 在客户端我只是值“新”但在服务器站点它打印未定义。
  • 在下面查看我的答案。如果它是一个字符串,你需要 JSON.stringify() 你的数据。
  • 好的,现在它的工作,解决方案是, var mydata = { data : $scope.createNewDirectory } 所以,我的变量声明是错误的。
  • 太好了,很高兴您能够让它工作。请点击旁边的复选标记接受我的回答,以便其他人知道它是正确的。

标签: angularjs node.js express


【解决方案1】:

默认情况下,$http 服务只会将请求数据转换为 JSON 字符串,前提是数据是对象。

https://github.com/angular/angular.js/blob/v1.4.8/src/ng/http.js#L292

在您的情况下,请求数据是一个字符串,因此它不会被引号包裹,从而使其无效 JSON。

您应该能够像这样修改您的 $http 配置:

$http({
    method: 'POST',
    url: '/create_new_directory',
    data: $scope.directory,
    transformRequest: JSON.stringify
}).success(function(data){
    console.log(data)
})

【讨论】:

    猜你喜欢
    • 2017-06-15
    • 2016-07-28
    • 2017-04-08
    • 2016-10-04
    • 2017-09-10
    • 1970-01-01
    • 2013-12-21
    • 2017-03-17
    • 2020-01-25
    相关资源
    最近更新 更多