我在react/javascript中有以下内容:
const uploadImage = (e) => {
e.preventDefault();
const uploadData = async () => {
const token = JSON.parse(localStorage.getItem('token'));
try {
data = await axios({
method: 'post',
url: 'http://192.168.1.218:8080/api/image/upload',
data: {
data: base64,
name: 'file',
type: 'image/jpeg'
},
headers: { "Content-Type": "multipart/form-data; boundary=<calculated when request is sent>",
"Authorization": `Bearer ${token}`
}
})
} catch (error) {
console.log(error)
}
}
uploadData()
}
但当我点击按钮时,控制台中会显示以下内容:
> xhr.js:177 POST http://192.168.1.218:8080/api/image/upload 400
dispatchXhrRequest @ xhr.js:177
xhrAdapter @ xhr.js:13
dispatchRequest @ dispatchRequest.js:52
Promise.then (async)
request @ Axios.js:61
wrap @ bind.js:9
uploadData @ Options.js:40
uploadImage @ Options.js:59
onSubmit @ Options.js:66
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js:4070
executeDispatch @ react-dom.development.js:8243
processDispatchQueueItemsInOrder @ react-dom.development.js:8275
processDispatchQueue @ react-dom.development.js:8288
dispatchEventsForPlugins @ react-dom.development.js:8299
(anonymous) @ react-dom.development.js:8508
batchedEventUpdates$1 @ react-dom.development.js:22396
batchedEventUpdates @ react-dom.development.js:3745
dispatchEventForPluginEventSystem @ react-dom.development.js:8507
attemptToDispatchEvent @ react-dom.development.js:6005
dispatchEvent @ react-dom.development.js:5924
unstable_runWithPriority @ scheduler.development.js:646
runWithPriority$1 @ react-dom.development.js:11276
discreteUpdates$1 @ react-dom.development.js:22413
discreteUpdates @ react-dom.development.js:3756
dispatchDiscreteEvent @ react-dom.development.js:5889
这个请求在 Postman 中确实有效。我尝试过添加标题,但这会导致更多错误。如果没有内容类型,则会出现500内部服务器错误。cors在后端启用。
我该怎么解决这个问题?非常感谢您的帮助。
附:这是我第一个问题。对不起,我错过了什么。
暂无答案!
目前还没有任何答案,快来回答吧!