利用XMLHttpRequest发起请求解决跨域

export default {
    data () {
        return {
            urlData: []
        }
    },
    mounted () {
        this.getData()
    },
    methods: {    
     // XmlHttpRequest对象
        createXmlHttpRequest () {
          if (window.ActiveXObject) { //如果是IE浏览器
            // eslint-disable-next-line no-undef
            return new ActiveXObject("Microsoft.XMLHTTP");
          } else if (window.XMLHttpRequest) { //非IE浏览器
            return new XMLHttpRequest();
          }
        },
        // 请求数据
        getData (searchObj) {
          let self = this
          var xhr = this.createXmlHttpRequest();
          xhr.open('GET', 'http://localhost:7777/supply/viewData', true);
          xhr.setRequestHeader('Content-Type', 'application/json');
          xhr.onload = function () {
            let result = JSON.parse(this.response)
            self.urlData = result.data
          }
          xhr.send(null);
        }
      }
    }
}

你可能感兴趣的:(前端杂烩,前端,vue.js)