前端项目 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(() => {
        })
相关推荐
艾莉丝努力练剑4 分钟前
【AI接入大模型SDK】Markdown格式解析
前端·c++·人工智能·学习·ai·架构
LinXunFeng1 小时前
A2UI 1.0 详解:让 AI 生成 Flutter 界面,还不怕它乱写
前端·flutter·agent
FRing0314 小时前
vue3干货总结3(defineOptions(Vue3.3)、defineModel(Vue3.4)、Pinia)
开发语言·前端·vue.js·vue3
Bczheng114 小时前
组件化服务端渲染模式SSR
前端
whitelbwwww15 小时前
CUDA C++实战--中级
开发语言·前端·c++
张紫娃15 小时前
Chrome插件-Stylus(屏蔽博客广告)
前端·chrome·stylus
Yannik16 小时前
在 Apple Silicon Mac 上用 Jenkins + Fastlane 搭建 iOS CI:从自动打包到 MR 检查流水线的完整实践
前端
Nayana16 小时前
《Web 到 HarmonyOS》-- 打包:Vite 产物 → HAP / HAR / HSP
前端
inksci16 小时前
用飞帆做了个小网页
前端·javascript·css·html
去伪存真16 小时前
被 pnpm 打包坑惨后,我彻底盘明白了硬链接与软链接
前端·electron