javascript - 我的提交功能以 Bad Gateway 502 响应
问题描述
我正在尝试为从我的 React 应用程序上传的图像设置“Cloudinary”。
我的提交函数不断响应:“Bad Gateway 502”和“SyntaxError: Unexpected end of input”。
我假设我的标题有问题,但我找不到问题...
handleFileSelect = (e) => {
this.formValid()
this.setState({
picture: e.target.files[0] })
}
submit(){
const CLOUDINARY_URL=
"https://api.cloudinary.com/v1_1/dvz27u2gu/image/upload"
const CLOUDINARY_UPLOAD_PRESET= "jshvp3nh"
const obj = Object.assign({}, this.state);
const formData = new FormData();
formData.append("file", obj.picture);
formData.append("upload_preset", CLOUDINARY_UPLOAD_PRESET);
fetch(CLOUDINARY_URL,{
mode: 'no-cors',
method:'post',
headers: { "Content-Type": "application/x-www-form-urlencoded"},
body:formData,
})
.then((res)=>{return res.json()})
.then(data=>console.log(data))
.catch(err=>console.log(err));
}
解决方案
您可以尝试以下方法:
<div><input type="file" onChange={this.submit}/></div>
submit = (e) => {
var file = e.target.files[0];
var data = new FormData();
data.append('upload_preset', 'jshvp3nh');
data.append('file', file);
data.append('cloud_name', 'dvz27u2gu');
const config = {
method: "POST",
body: data
};
var imgurl = "https://api.cloudinary.com/v1_1/dvz27u2gu/raw/upload";
fetch(imgurl, config)
.then(responseData => {
console.log('here');
console.log(JSON.stringify(responseData, null, 4));
console.log(responseData);
})}
推荐阅读
- c# - 当相机在 UNITY3D 的 X 轴上旋转 30 度时,如何让精灵跟随鼠标位置?
- c# - c# UWP并行图片加载
- apache-spark - 跨多个列搜索子字符串
- java - 使用 Jackson 将其反序列化为对象后,如何访问我的 GeoJSON 数据
- java - 数据库中的值每天递增一
- php - Symfony 4 - 将 VichUploader 与 Dompdf 一起使用?
- angular - Ionic 4 通过 GUI (ion-datetime) 自动更改日期设置
- c++ - 如何生成总和为 0.0 的 n 大小的随机浮点数组?
- javascript - 使用 JavaScript 在现有的本地 XML 中添加新元素
- spring-boot - Thymeleaf #aggregates.sum 具有多个条件