亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

axios 請求被阻止 | Reactjs

axios 請求被阻止 | Reactjs

撒科打諢 2023-12-14 16:56:47
我試圖使用我的reactjs代碼中的端點axios,但請求被阻止,并且我在控制臺中收到以下錯誤。請建議如何克服這個問題下面是我的代碼import * as React from 'react';import axios from "axios"export default function App(){  return (    <>      <button onClick={fetchdata}>Click Me</button>    </>  )}function fetchdata(){  const axios = require('axios');    // Make a request for a user with a given ID  return axios.get('https://randomuser.me/api')    .then(response => {      // handle success      console.log(response);      return response;    })    .catch(error => {      // handle error      console.log(error);  })}
查看完整描述

2 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

您收到此錯誤的原因是因為https://randomuser.me期望使用 https 協議來自安全來源的請求,并且默認情況下 localhost 不會通過 https 運行。如果不滿足這一點,您的瀏覽器將作為警告/錯誤返回。

但是,如果您使用以下方法生成了 React 項目,create-react-app則可以在 package.json 中添加以下腳本:

"proxy": "https://randomuser.me"


查看完整回答
反對 回復 2023-12-14
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

工作正常,請檢查這里

Codesandbox:https://codesandbox.io/s/muddy-dust-yvotv?file=/src/ App.js

有時出現此錯誤是因為您在瀏覽器中啟動了 cors 的任何插件/擴展


查看完整回答
反對 回復 2023-12-14
  • 2 回答
  • 0 關注
  • 241 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號