“未捕获(在承诺中)TypeError:无法读取未定义的属性(读取‘协议’)” React axios
2022-07-21
758
尝试从 API 获取数据时控制台中出现该错误。有人遇到过这个问题吗?
import React,{useState,useEffect} from 'react'
import "../App.css"
import axios from "axios"
const Main = () => {
const [loading,setLoading] = useState(false)
const [data,setData] = useState ([])
useEffect (() =>{
setLoading(true)
axios({
method:"GET",
URL:'https://fakestoreapi.com/products',
}).then(res => {
console.log (res.data)
setData(res.data)
})
.finally(()=> setLoading(false))
},[])
return (
<div></div>
)
}
export default Main
我收到的错误是:
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'protocol')
at isURLSameOrigin (isURLSameOrigin.js:57:1)
at dispatchXhrRequest (xhr.js:147:1)
at new Promise (<anonymous>)
at xhrAdapter (xhr.js:16:1)
at dispatchRequest (dispatchRequest.js:58:1)
at Axios.request (Axios.js:109:1)
at wrap (bind.js:9:1)
at Main.js:10:1
at commitHookEffectListMount (react-dom.development.js:23150:1)
at commitPassiveMountOnFiber (react-dom.development.js:24926:1)
有人知道吗
1个回答
将 URL 更改为 url
axios({
method:"GET",
url:'https://fakestoreapi.com/products',
})
如果以上代码不起作用,请尝试发送如下 GET 请求,
axios.get('https://fakestoreapi.com/products').then().catch().finally()
Mr Infinity
2022-07-21