gpt4 book ai didi

javascript - 我的提交功能以 Bad Gateway 502 响应

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:38:47 25 4
gpt4 key购买 nike

我正在尝试为从我的 React 应用上传图片设置“Cloudinary”。

我的提交函数不断响应:“Bad Gateway 502”和“SyntaxError:意外的输入结束”。

我假设我的标题有问题,但我找不到问题...

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);
})}

关于javascript - 我的提交功能以 Bad Gateway 502 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54637118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com