AxiosError:网络错误在XMLHttpRequest.handleError当运行在本地主机

xxls0lw8  于 2023-06-22  发布在  iOS
关注(0)|答案(1)|浏览(1017)

我得到这个网络错误,当我wnat得到API产品,这是我的代码:

import React, {useEffect} from 'react'
import axios from 'axios'

export default function App() {
  useEffect(() => {
   axios.get('https://api.digikala.com/v1/categories/mobile-phone/brands/apple/search/')
  .then(function(response){
  console.log(response)
  })
}, [])

return (
  <div>App</div>
)
}

这是我的错误:

ERROR
Network Error
AxiosError: Network Error
at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:40384:14)
ERROR
Network Error
AxiosError: Network Error
at XMLHttpRequest.handleError (http://localhost:3000/static/js/bundle.js:40384:14)
wqsoz72f

wqsoz72f1#

添加catch,像这样:

axios.get('https://api.digikala.com/v1/categories/mobile-phone/brands/apple/search/').then(function(response) {
    console.log(response)
}).catch((err) => {
    console.log(err)
})

相关问题