0
您好我想在我的反应应用程序中使用axios发布多部分数据,但它不会得到张贴和花费很长时间后给我错误差不多多5分钟“(失败)的净::: err_CONNECTION_RESET,我不知道是怎么回事错在这里,下面是代码片段coudn't上传多部分数据的反应应用程序使用axios
功能,使reqeuset:
handleClick(event){
let self = this;
if(this.state.filesToBeSent.length>0){
const formData = new FormData();
formData.append('file',this.state.filesToBeSent[0][0]);
let jsonObject = new Object;
jsonObject.itemId="1262";
jsonObject.module="Breakfix";
jsonObject.filename=("yyyyy");
jsonObject.filepath="c:\\abc\\";
jsonObject.createdOn=Math.round(new Date().getTime()/1000.0);
jsonObject.createdBy="3";
formData.append("attachment", JSON.stringify(jsonObject));
let filesArray = this.state.filesToBeSent;
axios.post(GLOBAL.uploadFile,
formData
);
}
else{
alert("Please upload some files first");
}
}
**code written on express to route the post to the actual API** :
function uploadFiles(request, response) {
let payload = request.signedCookies['access_token'];
payload.apikey = "d2c-234";
const secret = 'my-secret';
const signed = jwt.sign(payload, secret, {
algorithm: 'HS256',
expiresIn: '7d'
});
let config = {
headers: {'x-auth-token': signed
}
};
let data={}
if(!_.isEmpty(request.body)) {
data = request.body;
}
axios.post("https://abc-myapp.net/serviceManagement/rest/uploadBreakfixImage/",data,config)
.then(uploadResponse => {
response.status(200).json(uploadResponse);
}).catch(function(error) {
console.error(error.stack);
});
}
when putting console on the express side it seems request doesn't have the payload, what am i doing wrong here ??
嗨@Ravindra,我正在串化该JSON对象,把所有的关键字命名为“附件”,然后再解析它在API端使用JSON解析器,它工作正常,当我试图通过POSTMAN客户端提交数据。 –
所以,您的意思是,您将整个JSON内容作为一个键值对中的字符串发送给名为附件的键。然后检查HTTP头并在这里发布。所以我可以帮助你。 –
接受方:application/json,text/plain,*/* Content-Length:196667 Content-Type:multipart/form-data; border = ---- WebKitFormBoundarymCXU9DZi4uU9kQA4 Cookie:access_token ='这里是我的访问令牌' –