前端项目 index.html 中发请求 fetch

想要在前端项目 index.html文件中向后端发起请求,但是引入axios报错(我这边会报错),可以使用fetch

javascript 复制代码
//window.location.origin----获取域名,包括协议、主机号、端口号
      fetch(window.location.origin + "/api/plant-consumer/plant/getGeoInfoByOutNetIp?outerNetIp=", {
        method: "GET", // 请求方法
        withCredentials: false, //是否开启跨域,开启后可以设置自定义的header头,但是后端要开启*以及允许哪些header头
        headers: { // 填写headers请求头
          "Content-Type": "application/x-www-form-urlencoded",
          Authorization: localStorage.getItem("Authorization"),
        },
      })
        .then(resp => resp.json())
        .then(res => {
        
        })
        .catch(() => {
        })
相关推荐
IT_陈寒1 小时前
Vite静态资源加载把我坑惨了
前端·人工智能·后端
herinspace1 小时前
管家婆实用贴-如何分离和附加数据库
开发语言·前端·javascript·数据库·语音识别
小码哥_常2 小时前
从MVC到MVI:一文吃透架构模式进化史
前端
嗷o嗷o2 小时前
Android BLE 的 notify 和 indicate 到底有什么区别
前端
豹哥学前端2 小时前
别再背“var 提升,let/const 不提升”了:揭开暂时性死区的真实面目
前端·面试
lar_slw2 小时前
k8s部署前端项目
前端·容器·kubernetes
拉拉肥_King2 小时前
Ant Design Table 横向滚动条神秘消失?我是如何一步步找到真凶的
前端·javascript
GreenTea2 小时前
DeepSeek-V4 技术报告深度分析:基础研究创新全景
前端·人工智能·后端
河阿里3 小时前
HTML5标准完全教学手册
前端·html·html5
吴声子夜歌3 小时前
Vue3——新语法
前端·javascript·vue.js