我有一個 React ui,它應該與后端通信并獲取資料。后端運行良好,這是它對我的請求的回應:
當我在 React 中針對同一請求收到此錯誤時。url 是一樣的,引數已經到位,但仍然有錯誤,我無法捕獲輸出:

這是我在本節中基于 AXIOS.POST 的代碼:
const handleSubmit = (e) => {
e.preventDefault()
const params = { petalLength, petalWidth, sepalLength, sepalWidth }
axios.post("http://localhost:8000/iris/classify/class", {},{params:params})
.then((res) => {
const data = res.data.data
console.log(data)
const msg = `Prediction: ${data}`
alert(msg)
reset()
})
.catch((error) => alert(`Error: ${error.message}`))
}
const reset = () => {
setPetalLength('')
setPetalWidth('')
setSepalLength('')
setSepalWidth('')
}
即使采用以下形式也行不通:
>
> axios.post("http://localhost:8000/iris/classify/class?petalLength=1&petalWidth=1&sepalLength=1&sepalWidth=3.1")
> .then((res) => {
> const data = res.data.data
> console.log(data)
> const msg = `Prediction: ${data}`
> alert(msg)
> reset()
> })
> .catch((error) => alert(`Error: ${error.message}`)) }
>
> const reset = () => {
> setPetalLength('')
> setPetalWidth('')
> setSepalLength('')
> setSepalWidth('') }
uj5u.com熱心網友回復:
在您使用 GET 請求的第一張圖片中,
在您的代碼中,您正在使用 POST 請求
這兩個請求之間存在差異。這就是為什么服務器告訴您它無法在您的代碼中處理它,但是當您在 url 中使用它時它可以正常作業。
將其更改為
> axios.get("http://localhost:8000/iris/classify/class?petalLength=1&petalWidth=1&sepalLength=1&sepalWidth=3.1")
> .then((res) => {
> const data = res.data.data
> console.log(data)
> const msg = `Prediction: ${data}`
> alert(msg)
> reset()
> })
> .catch((error) => alert(`Error: ${error.message}`)) }
祝你好運 (:
順便說一句,您的網站看起來不錯(:
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460485.html
下一篇:當我使用express路由器時,req.body回傳undefined,即使我使用express.urlencoded
