首页 文章

反应:Axios网络错误

提问于
浏览
9

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

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

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

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

1 回答

  • 9

    如果使用NodeJS创建API


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

    // This should already be declared in your API file
    var app = express();
    
    // ADD THIS
    var cors = require('cors');
    app.use(cors());
    

    要更全面地了解CORS,请阅读Mozilla Documentation on CORS .

相关问题