使用 axios 进行 get 请求时出现 404

时间:2021-05-19 11:54:27

标签: react-native axios

我收到了一个 404 请求:

import axios from "axios";

axios
      .get("https://api-public.sandbox.pro.coinbase.com")
      .then((data) => console.log(data.json()));

错误:


Uncaught (in promise) Error: Request failed with status code 404
    at createError (createError.js:16)
    at settle (settle.js:17)
    at XMLHttpRequest.handleLoad (xhr.js:61)
createError @ createError.js:16
settle @ settle.js:17
handleLoad @ xhr.js:61
Promise.then (async)
(anonymous) @ App.js:75
invokePassiveEffectCreate @ react-dom.development.js:23487
callCallback @ react-dom.development.js:3945
invokeGuardedCallbackDev @ react-dom.development.js:3994
invokeGuardedCallback @ react-dom.development.js:4056
flushPassiveEffectsImpl @ react-dom.development.js:23574
unstable_runWithPriority @ scheduler.development.js:468
runWithPriority$1 @ react-dom.development.js:11276
flushPassiveEffects @ react-dom.development.js:23447
(anonymous) @ react-dom.development.js:23324
workLoop @ scheduler.development.js:417
flushWork @ scheduler.development.js:390
performWorkUntilDeadline @ scheduler.development.js:157

这是因为cors。解决此问题的最佳方法是什么? 这是因为cors。解决此问题的最佳方法是什么?

2 个答案:

答案 0 :(得分:-1)

  1. 使用此服务器 https://cors-anywhere.herokuapp.com/ 作为您请求的代理,它为我解决了问题,有关它的更多详细信息 click here

  2. 或者你可以尝试使用 fetch api

  3. 我建议您使用该 Axios 请求的捕获来获取有关错误的更多详细信息

答案 1 :(得分:-1)

试试下面的代码;

import axios from 'axios';    

//Create Axios instance
const Instance = axios.create({
    baseURL: "https://api-public.sandbox.pro.coinbase.com",
    timeout: 15000,
    timeoutErrorMessage: 'Your request Timed-out',
    headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }
});
//Export the Instance if needed


//From a function, use the instance
Instance.get("")
      .then((data) => console.log(data.json()));