【发布时间】:2016-06-27 12:58:17
【问题描述】:
我不确定客户端或服务器上的代码哪里出错了?如果我不发送 FormData(req.body 读取信息),它会起作用,但是一旦我将其更改为 FormData,因为我试图发送图片和一些字符串,我在标题的服务器端收到错误 500。长度控制台.log
使用我当前的正文解析器的 Req.body 不会提取 AJAX JS 请求发回的 FormData...
这是我的 JS 端:
var newProjectImage = $("#filebutton").get(0).files[0];
var formData = new FormData();
formData.append('picture', newProjectImage);
formData.append('title', newProjectTitle.trim());
$.ajax({
type: "POST",
url: "/adminAddProject",
data: formData,
cache: false,
dataType: 'json',
processData: false, // Don't process the files
contentType: false, // Set content type to false as jQuery will tell the server its a query string request
success: function (data) {
if(data) {
alert("Product added");
successAddProject(data);
enableAddProjectButtons();
} else {
alert("data does not exist ");
enableAddProjectButtons();
}
console.log("the data returned is " + JSON.stringify(data));
}
});
这是 NodeJS 端:
我的 app.js 有这些用于 body-parse:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
那么路线是这样的:
router.post('/adminAddProject', function(req, res) {
console.log("Called admin add " + JSON.stringify(req.body));
if(req.cookies.token) {
console.log("Called admin token passes");
if(req.body) {
console.log("Called admin body is real ");
var title;
title = req.body.title;
console.log("Called admin body is real " + title.length);
【问题讨论】:
-
没人解释,我特意找了一个有数据上传的文件
标签: javascript jquery ajax node.js file-upload