vue项目里访问第三方接口如天气接口、获取IP接口
文章目录天气接口IP1、index.html里面写script【推荐】2、用js间接写。。天气接口这里用的是这个:https://www.tianqiapi.com请求接口:getDateWeatherAir (ip) {return $http({url: `https://www.tianqiapi.com/api/?appid=347585&app...
   ·  
 天气接口
这里用的是这个:https://www.tianqiapi.com
  默认返回当前IP地区天气
  请求接口:
  getDateWeatherAir () {
    return $http({
      url: `https://www.tianqiapi.com/参数`,
      method: 'get',
    });
  },
数据举例:
  方法:
store.getDateWeatherAir(ip).then(res => {
        if (res) {
          this.weather.today = res.data[0];
          this.weather.tmr = res.data[1];
        }
      });
IP
获取IP地址,可以用搜狐的这个
http://pv.sohu.com/cityjson?ie=utf-8

1、index.html里面写script
在index.html中直接写
<script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>
<script type="text/javascript">
    sessionStorage.setItem('ip', returnCitySN["cip"]);
</script>
在页面中直接取let ip = sessionStorage.getItem('ip');
  这种方法不是很推荐,会导致在每个页面都访问该外部接口
2、用js间接写。。
export default function getIP () {
  let script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'http://pv.sohu.com/cityjson?ie=utf-8';
  document.head.appendChild(script);
  let script2 = document.createElement('script');
  script2.type = 'text/javascript';
  script2.innerText = 'sessionStorage.setItem(\'ip\', returnCitySN["cip"]);';
  document.head.appendChild(script2);
}
但是容易报错returnCitySN is not defined
更多推荐
 
 



所有评论(0)