Javascript 使用'fetch'或'request'发送多部分/表单数据的正确方法`
这是我要发送到服务器的数据结构:Javascript 使用'fetch'或'request'发送多部分/表单数据的正确方法`,javascript,ajax,reactjs,fetch,Javascript,Ajax,Reactjs,Fetch,这是我要发送到服务器的数据结构: { attachment: [File], foo: String, bar: String } 如您所见,我正在尝试发送一组文件以及一些其他数据。为了存储所有这些数据,我使用了JavaScript官方API中提供的FormData构造函数。我是这样填充formData的: for (let i = 0; i < this.state.files.length; i++) { let f = this.state.files[i
{
attachment: [File],
foo: String,
bar: String
}
如您所见,我正在尝试发送一组文件以及一些其他数据。为了存储所有这些数据,我使用了JavaScript官方API中提供的FormData构造函数。我是这样填充formData的:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
没有太多的成功。方法是POST。authHeaderauthToken仅生成授权:承载者。。。。问题是我认为得到指定的头会被我的身份验证头覆盖
所以我尝试使用request和request-promise-native。我做了这样的事情:
rp({
url,
method,
headers: {
...authHeader(authToken)
},
formData: data
});
结果相似。使用授权标头和FormData中的文件数组发送此类数据的正确方法是什么?这是fetch对象中可用的选项
fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
// "Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
如果需要向服务器发送一些自定义头,只需按如下方式编写:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
由于要发送多部分表单数据,只需将数据添加到请求主体中,如下所示:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
如果表单标记中包含字段,则可以如下设置表单数据:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
如果您使用http身份验证,则有不同的身份验证方案,请使用此链接作为参考
如果您使用的是基本授权,则应使用以下内容:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
这是fetch对象中可用的选项
fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json",
// "Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data), // body data type must match "Content-Type" header
})
如果需要向服务器发送一些自定义头,只需按如下方式编写:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
由于要发送多部分表单数据,只需将数据添加到请求主体中,如下所示:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
如果表单标记中包含字段,则可以如下设置表单数据:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
如果您使用http身份验证,则有不同的身份验证方案,请使用此链接作为参考
如果您使用的是基本授权,则应使用以下内容:
for (let i = 0; i < this.state.files.length; i++) {
let f = this.state.files[i];
this.formData.append('attachment', f, f.name);
}
this.formData.append('foo', this.state.foo);
this.formData.append('bar', this.state.bar);
headers: {
"My-Custom-Header": "Custom-Header-Value",
}
body: formData
var formData = new FormData(document.querySelector("form"));
headers: {
'Authorization': 'Basic '+btoa('username:password')
}
为了将来参考,我使用以下配置发送了数据:
fetch(url, {
method: method,
body: data,
headers: {
'Accept': 'application/json',
...authHeader(authToken)
}
})
也就是说,通过Accept头似乎解决了这个问题。还请注意,未设置内容类型标题,允许浏览器自行设置。为了将来参考,我使用以下配置发送了数据:
fetch(url, {
method: method,
body: data,
headers: {
'Accept': 'application/json',
...authHeader(authToken)
}
})
也就是说,通过Accept头似乎解决了这个问题。还请注意,未设置内容类型标题,允许浏览器自行设置。此答案涵盖了我已经编写的大部分内容。缺少的部分是:“Accept”:“application/json”,一切都很好。关于您的HTTP身份验证,我已经写过我使用authHeader处理它。“谢谢你。”德拉戈斯·特鲁加我很高兴你发现了你的问题。使用本机fetch时,您需要自己设置一些头。其他http客户端(如axios)为您管理这些头文件,但本机Feccch的情况并非如此。缺少的部分是:“Accept”:“application/json”,一切都很好。关于您的HTTP身份验证,我已经写过我使用authHeader处理它。“谢谢你。”德拉戈斯·特鲁加我很高兴你发现了你的问题。使用本机fetch时,您需要自己设置一些头。其他http客户机(如axios)为您管理这些头,但本机Feccch的情况并非如此。