在客户端我有:
var data = new FormData();
data.append('users', [{ name: "John"}] );
data.append('excel', files[0]);
console.log(data);
$.ajax({
url: 'url',
type: 'POST',
data: data,
cache: false,
processData: false,
contentType: false,
在服务器上我的下一个中间件:
app.use(bodyParser.json({limit:1024*1024}));
app.use(bodyParser.urlencoded());
app.use(multer());
但是当我发送一些东西时,我有文件但没有解析 JSON 正文,它看起来像:
{ users: '[object Object]' }
最佳答案
我认为你应该显示 Controller 代码。但根据你在评论中所说的。
And if i user JSON.stringify([{ name: "John"}]), i will be have this { users: '[{"name":"John"}]' }
用途:
JSON.stringify([{ name: "John"}])
然后在接收端:
{ users: JSON.parse('[{"name":"John"}]') }
对接收到的数据使用 JSON.parse
。
现在你:
- 以字符串形式发送数据
- 以字符串形式接收数据
- JSON.parse 接收的字符串将其转换回对象
关于javascript - 发送带有 JSON 正文的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26384030/