让前端飞Web前端之路前端开发笔记

vue spa应用加入ip访问统计功能(前端)

2018-12-05  本文已影响17人  李佳明先生

最近做网站的时候有需求,是在前端加入用户省份收集统计功能。
这里我们使用搜狐的接口,这个接口适用于所有平台及浏览器:

  1. 在入口页面index.html里添加如下代码(返回的数据里包含ip地址和省份):

  <!-- MAIN JS -->
  <script src="./static/js/main.js"></script>
  <!--加入ip统计代码-->
  <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script>
</html>
  1. 在index.vue里的mounted里加入代码:
mounted:function () {
      var that=this;
      //等待js都加载完之后,调用countip方法
      window.onload=function (){
        that.countIp()
      }
    },
  1. 发送给后台,让后台去做处理:
countIp:function(){
        axios.post(api.countIp,returnCitySN).then(function (res) {
          console.log(res);
        }).catch(function (error) {
          console.log(error);
        })
      },
上一篇 下一篇

猜你喜欢

热点阅读