首页 > 其他分享 >vue 使用天地图地理编码API 报跨域错误: from origin 'http://localhost' has been blocked by CORS policy: Re

vue 使用天地图地理编码API 报跨域错误: from origin 'http://localhost' has been blocked by CORS policy: Re

时间:2023-01-31 18:23:39浏览次数:41  
标签:origin Control res Access header formStr Allow

 

解决办法

加了一个header 头之后可以正常访问  "Access-Control-Allow-Origin": "*", ;

      axios
        .get(
          `${this.addressBDApi}geocoder?ds={"keyWord":"${
            this[this.formStr].address
          }"}&tk=5761188b4211edf82307015479d92b4b`,
          {             headers: {               "Access-Control-Allow-Origin": "*",  //加上这个请求头之后可以正常访问             },           }
        )
        .then((res) => {
          if (res.status == 200) {
            this[this.formStr].latitude = res.data.location.lat.toFixed(3);
            this[this.formStr].longitude = res.data.location.lon.toFixed(3);
            console.log('经纬度',this[this.formStr].latitude,this[this.formStr].longitude)
          } else {
            this[this.formStr].latitude = null;
            this[this.formStr].longitude = null;
            this.msgError("未查询到经纬度");
          }
        });

 好像不同版本的axios 默认的请求头不一样,之前用的axios 不用加这个请求头也可以正常访问。

标签:origin,Control,res,Access,header,formStr,Allow
From: https://www.cnblogs.com/esmusssein1/p/17080139.html

相关文章