前端项目 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(() => {
        })
相关推荐
前端fighter12 小时前
2026年前端面试题总结(pdf版)
前端·pdf
谢小飞12 小时前
运维加了个参数,我们的网页挂掉了
前端·浏览器
CoderYanger13 小时前
Java EE 进阶:12.Spring AOP(第二弹)
java·前端·数据库·spring boot·spring·java-ee·mybatis
用户693717500138416 小时前
大模型蒸馏到底在蒸什么?一篇文章搞懂知识蒸馏概念
前端·后端·github
徐小夕16 小时前
实测 10 个前端 UI 生成开源 Skill|AI 输出直达上线标准
前端·vue.js·github
tong_li16 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(八):AI 知道这字哪课学的(SQLite 结构化查询入门)
前端·langchain
用户693717500138417 小时前
当 Claude 开始反对蒸馏,我想起了那 700 万份盗版书籍
前端·后端·面试
zhanlang021717 小时前
HTML 零基础入门到企业实战全套教程【10.1】综合实战|从零开发静态企业官网:需求分析、页面结构规划、素材准备
前端·html
Bczheng117 小时前
浏览器访问本地html和服务器html的流程
前端
金八梭17 小时前
格斗对决v4.5
前端·css·自动化·html·css3