前端项目 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(() => {
        })
相关推荐
huohaiyu2 小时前
从URL到页面的完整解析流程
前端·网络·chrome·url
阿星AI工作室4 小时前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin4 小时前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海6 小时前
JSBridge 原理详解
前端
孟健6 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion6 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者8 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p3658 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
享誉霸王9 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5
a11177610 小时前
飞机躲避炸弹 网页游戏
前端·开源·html·threejs