[英]Incorrect cloudinary response of type "cors" after uploading using REST API (from React)
I am trying to upload an image using direct REST API call using the code below.我正在尝试使用以下代码使用直接 REST API 调用上传图像。 Code works from my local pc (localhost:3000).代码在我的本地电脑 (localhost:3000) 上工作。
const cloudinaryUrl = 'https://api.cloudinary.com/v1_1/{my_id}/image/upload';
const imageUrl = 'https://{test_image_url}/images/{image}.jpg';
const saveImage = async () => {
try {
const fd = new FormData();
fd.append('file', imageUrl);
fd.append('upload_preset', 'my_preset');
fd.append('tags', 'browser_upload');
const image = await fetch(`${cloudinaryUrl}`, {
method: 'POST',
body: fd
});
console.log('Response', image);
} catch (err) {
console.log('err', err);
}
};
The image is saved successfully but the response is not correct.图像保存成功,但响应不正确。 The response is below:回应如下:
{
body: "",
type: "cors",
bodyUsed: false,
status: 200,
statusText: "OK",
url: "https://api.cloudinary.com/v1_1/{my_id}/image/upload"
}
Correct response is supposed to be something like this:正确的反应应该是这样的:
{
public_id: 'sample',
version: '1312461204',
width: 864,
height: 564,
format: 'jpg',
created_at: '2017-08-10T09:55:32Z',
resource_type: 'image',
tags: [],
bytes: 9597,
type: 'upload',
etag: 'd1ac0ee70a9a36b14887aca7f7211737',
url: 'http://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg',
secure_url: 'https://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg',
signature: 'abcdefgc024acceb1c1baa8dca46717137fa5ae0c3',
original_filename: 'sample'
}
If anyone knows how to solve my issue, it would be great.如果有人知道如何解决我的问题,那就太好了。
You can get the response body by calling the promise-based
methods https://javascript.info/fetch您可以通过调用promise-based
方法来获取响应正文https://javascript.info/fetch
For example, you can use the method json()
to parse the response as JSON
.例如,您可以使用json()
方法将响应解析为JSON
。
const image = await fetch(url, { method: "POST", body: formData });
const result = await image.json();
console.log('Response', result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.