我正在发送包含以下详细信息以及多个图像作为表单数据的帖子:
let form = new FormData();
form.append('img1', imgArray[0]);
form.append('img2', imgArray[1]);
form.append('img3', imgArray[2]);
form.append('price', selectedprice);
form.append('seller_id', id);
form.append('type', selectedvalue);
form.append('category', 'care');
form.append('from_date', startdate);
form.append('to_date', enddate);
form.append('discription', description);
console.log('form data====>', form);
var requestOptions = {
method: 'POST',
headers: {
Accept: 'application/json',
'content-type': 'multipart/form-data',
},
body: form,
redirect: 'follow',
};
fetch('http://base_url/api/upload-products', requestOptions)
.then((response) =>
response.json()
).then(res => console.log('UPLOAD res===>', res))
.catch(error =>
console.log('error==catch>', error));
});
使用此 API 后,我在 catch 块中收到以下错误:
[SyntaxError: JSON Parse error: Unexpected token: N]
然后 API 响应 400,但是,这个 API 在 Postman 中运行良好。之前我使用此 API 发送单个图像,后来我们也使用多个图像更新了 API,但这次我遇到了问题。
这就是我的 formData 的样子:
{"_parts": [["img1", "file:///data/user/0/com.appName/cache/rn_image_picker_lib_temp_7eb48acb-2b5e-4ed9-9b20-b7d8293001cd.jpg"], ["img2", "file:///data/user/0/com.appName/cache/rn_image_picker_lib_temp_a8241f47-2d9c-4717-8994-e711a8f22dad.jpg"], ["img3", "file:///data/user/0/com.appName/cache/rn_image_picker_lib_temp_b9a00c94-03de-43eb-ac08-1e4a3a38a22b.jpg"], ["price", "79"], ["seller_id", "65c71746fcf33d49d3334d10"], ["type", "machine"], ["category", "health care"], ["from_date", "17-04-2024"], ["to_date", "17-04-2024"], ["discription", "Ns"]]}
请在这里帮助我。谢谢
我需要将 formData 中的图像发送更新为:
{uri: photo.uri, name: 'image.jpg', type: 'image/jpeg'}
因此,一旦添加图像离子表单数据,它就应该与所有这些值一起作为一个对象。 最终表格数据:
{"_parts": [["img1", [Object]], ["img2", [Object]], ["img3", [Object]], ["price", "55"], ["seller_id", "65c71746fcf33d49d3334d10"], ["type", "machine"], ["category", "health"], ["from_date", "18-04-2024"], ["to_date", "18-04-2024"], ["discription", "Bbb"]]}