Javascript 如何在react中使用axios发送文件和数据?
后端正在期待类似的东西Javascript 如何在react中使用axios发送文件和数据?,javascript,reactjs,axios,Javascript,Reactjs,Axios,后端正在期待类似的东西 { "key" : "xyz" }// and two files 我尝试了以下代码,但它总是显示在url中传递的空参数 const obj = { key : [this.state.key] }; const json = JSON.stringify(obj); const blob = new Blob([json], { type
{
"key" : "xyz"
}// and two files
我尝试了以下代码,但它总是显示在url中传递的空参数
const obj = {
key : [this.state.key]
};
const json = JSON.stringify(obj);
const blob = new Blob([json], {
type: 'application/json'
});
const data = new FormData();
data.append("params", blob);
data.append("data1",this.state.file1)
data.append("data2",this.state.file2)
let res= await axios.get(url,data);
console.log(res)
无法使用get方法发送数据(正文)。我认为你需要使用post方法
const obj={
key:[这个.state.key]
};
const json=json.stringify(obj);
const blob=new blob([json]{
键入:“application/json”
});
const data=新表单数据();
数据。追加(“参数”,blob);
data.append(“data1”,this.state.file1)
data.append(“data2”,this.state.file2)
let res=等待axios.post(url,数据)代码>您不能将formData与get一起使用,为了与正文一起发送,文件上载必须是post请求
let res= await axios.post(url,data);
另一个来源:您可以通过完整的react示例找到另一个来源。