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

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

//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(() => {
        })

你可能感兴趣的:(前端,html)