Javascript 如何将包含文件的json对象发送到Nodejs?

Javascript 如何将包含文件的json对象发送到Nodejs?,javascript,jquery,json,ajax,node.js,Javascript,Jquery,Json,Ajax,Node.js,我正在使用Nodejs作为服务器端开发这个简单的表单应用程序。我能够接收Ajax调用发送给Node的JSON对象数据。但我的问题是,当我向JSON对象添加一个新属性,允许我上载一个文件(数据位置:$('input[type=file]')[0]。files)时,服务器无法获取该属性,并抛出一个错误 那么,如何将保存文件值的新属性附加到json对象,以便服务器能够接收它呢?或者我需要改变我的Ajax调用吗 这是我的密码: script.js $("form").submit( function()

我正在使用Nodejs作为服务器端开发这个简单的表单应用程序。我能够接收Ajax调用发送给Node的JSON对象数据。但我的问题是,当我向JSON对象添加一个新属性,允许我上载一个文件(数据位置:
$('input[type=file]')[0]。files
)时,服务器无法获取该属性,并抛出一个错误

那么,如何将保存文件值的新属性附加到json对象,以便服务器能够接收它呢?或者我需要改变我的Ajax调用吗

这是我的密码:

script.js

$("form").submit( function() {
hideError();
submit = submitAnswers();
if(submit == true){
  var jsonData = createJson();
  $.ajax({
    url: '/form',
    type: 'POST',
    dataType: 'json',
    data: jsonData,
    // enctype: 'multipart/form-data',
    // //contentType: 'application/json',
    error: function(err){
      console.log(err);
      showError(err.responseText);
    }
  });
}
});

function createJson(){
var answers = $("form[name = quizForm] input[type=radio]:checked")
var results = {
  authority:     answers[0].value == 'a',
  sharing:       answers[1].value == 'a',
  accuracy:      answers[2].value == 'a',
  quality:       answers[3].value == 'a',
  complete:      answers[4].value == 'a',
  format:        answers[5].value == 'a',
  type:          answers[6].value == 'a',
  ownership:     answers[7].value == 'a'
  datalocation:  $('input[type=file]')[0].files
}
return results;
}
服务器代码

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
res.sendfile('./public/html/form.html');
});

router.post('/', function(req, res, next) {

/*This verifies that json data was sent to Server after being executed by the Ajax call*/
console.log('Request received: ');
console.log(req.body) // this line helps you inspect the request so you can see whether the data is in the url (GET) or the req body (POST)
console.log('\nRequest recieved: \nmethod: ' + req.method + '\nurl: ' + req.url) // this line logs just the method and url

res.end('callback(\'{\"msg\": \"OK\"}\')');

使用FileReader获取内容或使用FormData代替JSONWell JSON类型不能包含文件。你应该发布一个由多部分组成的请求。