我一直在浏览无数帖子,而我对此感到很困惑,应该很简单。
let formData = new FormData();
formData.append('message', payload.message);
formData.append('attachment', this.state.attachment);
let options = {
method: 'post',
body: formData;
}
fetch('api/contact', options);
然后我得到了非常熟悉的415错误:
Request URL: https://localhost:44348/api/contact
Request Method: POST
Status Code: 415
Remote Address: [::1]:44348
Referrer Policy: no-referrer-when-downgrade
我不知道不设置我的Content-Type,甚至不设置标题,包括边界在内的所有内容看起来都很好:
请求标题
:authority: localhost:44348
:method: POST
:path: /api/contact
:scheme: https
accept: */*
accept-encoding: gzip, deflate, br
accept-language: en-US,en;q=0.9
cache-control: no-cache
content-length: 10269
content-type: multipart/form-data; boundary=----WebKitFormBoundary5iTqTV6AaKla3BMA
origin: https://localhost:44348
pragma: no-cache
referer: https://localhost:44348/contact
sec-fetch-dest: empty
sec-fetch-mode: cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36
表格数据
message: asdasd
attachment: (binary)
这是我以前在旧jQuery网站上使用的同一API,并且运行良好。有什么想法吗?
let options = {
method: 'post',
body: formData
}
JSON声明中;
后的分号body
可能会导致运行错误。这可能是一个问题,请检查一下。在附加附件时,请检查附件是否正确转换为二进制格式。希望能帮助到你!!编码愉快!