Reactjs 如何通过react将数据传递到postman中的表单数据类型中包括文件?
首先,我是新手,需要通过API将数据从前端传递到后端。数据的格式为邮递员提供的数据。下图显示了POSTMAN中的API工作 我在这里使用表单数据作为api数据 问题就在这里。 因为这是我第一次处理将文件传递到表单数据的问题。下面的代码是我要做的:Reactjs 如何通过react将数据传递到postman中的表单数据类型中包括文件?,reactjs,api,fetch,form-data,Reactjs,Api,Fetch,Form Data,首先,我是新手,需要通过API将数据从前端传递到后端。数据的格式为邮递员提供的数据。下图显示了POSTMAN中的API工作 我在这里使用表单数据作为api数据 问题就在这里。 因为这是我第一次处理将文件传递到表单数据的问题。下面的代码是我要做的: constructor (props){ super(props); const supplier_id = parseInt(localStorage.getItem('id')); this.st
constructor (props){
super(props);
const supplier_id = parseInt(localStorage.getItem('id'));
this.state ={
supplier_id: supplier_id,
item_name:'',
item_shortDes: '',
item_longDes: '',
price: '',
terms_agreement: '',
Location: '',
selectedFile: null, // The item file which I need to pass to api.
redirect: false,
}
this.onChange = this.onChange.bind(this);
this.createItem = this.createItem.bind(this);
}
createItem(){
console.log(this.state)
fetch(`http://localhost:9000/api/item/submit`, { // callling the API here
method: 'post',
body: JSON.stringify(this.state)
}).then ((result) => {
let responseJSON = result;
console.log(responseJSON);
});
}
我在这里得到了错误:
所以我相信这是因为Im没有传递
表单数据类型。我说得对吗?我将如何处理这个问题?首先需要发送表单数据,您必须像下面那样调用表单数据类
formData=newformdata()
然后开始附加表单数据
formData.append(“keyName”,
${keyName})