二、遇到的问题想要实现的效果:获取到图片本地内容后,通过 fetch 来上传 formData 格式数据。
有问题的请求:
function request(params){
const { api, data } = params;
const url = window.location.origin + api;
return fetch(url,{
method:"POST",
headers:{
'Content-Type': 'multipart/form-data',
},
body: data
}).then((response) => response.json())
}
三、针对fetch 的解决方法结果服务器报错:message: "Failed to parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
这不是接口想要的类型
试过更改 Content-Type 的值,【multipart/form-data;】【application/x-www-form-urlencoded】
结果可能是这样
四、总结 使用form-data提交的时候,不要手动设置content-type
最终还是解决不了, 最后直接删掉了headers,让客户端浏览器自动识别生成headers ,便成功了。
解决办法:删掉 headers 让浏览器自动识别添加
fetch(url,{ method:"POST", body: formdata})
.then((response) => response.json())
如下



