反应:Axios 网络错误 [英] React: Axios Network Error

查看:28
本文介绍了反应:Axios 网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用axios,遇到了一个错误.

This is my first time using axios and I have encountered an error.

  axios.get(
    `http://someurl.com/page1?param1=1&param2=${param2_id}`
  )
  .then(function(response) {
    alert();
  })
  .catch(function(error) {
    console.log(error);
  });

使用正确的 url 和参数,当我检查网络请求时,我确实从我的服务器得到了正确的答案,但是当我打开控制台时,我看到它没有调用回调,而是捕获了一个错误.

With the right url and parameters, when I check network requests I indeed get the right answer from my server, but when I open console I see that it didn't call the callback, but instead it caught an error.

错误:网络错误堆栈跟踪:createError@http://localhost:3000/static/js/bundle.js:2188:15handleError@http://localhost:3000/static/js/bundle.js:1717:14

Error: Network Error Stack trace: createError@http://localhost:3000/static/js/bundle.js:2188:15 handleError@http://localhost:3000/static/js/bundle.js:1717:14

推荐答案

如果使用 NodeJS 创建 API

<小时>您的 Express 应用程序需要使用 CORS(跨域资源共享).将以下内容添加到您的服务器文件中:

If Creating an API Using NodeJS


Your Express app needs to use CORS (Cross-Origin Resource Sharing). Add the following to your server file:

// This should already be declared in your API file
var app = express();

// ADD THIS
var cors = require('cors');
app.use(cors());

要更全面地了解 CORS,请阅读 关于 CORS 的 Mozilla 文档.

For fuller understanding of CORS, please read the Mozilla Documentation on CORS.

这篇关于反应:Axios 网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆