最近在写vue项目的过程中,需要调用来自其他网站提供的接口,但是存在跨域问题,以下方法使用原生XMLHttpRequest可以解决跨域问题,亲测可行:
let that = this
let xhr = new XMLHttpRequest()
xhr.open('GET', 'https://xxx.com/api')
xhr.responseType = 'json'
xhr.send()
xhr.onload = function () {
that.loading = false
if (xhr.status === 200) {
let resp = xhr.response
console.log(resp)
} else {
console.log('Error')
}
};