由于jsonp跨域是异步的,导致在ajax里设置async:false也根本没有用,想问问大家有没有什么好的方法能够在既使用jsonp+ajax跨域,又能够解决异步带来的问题?请赐教,不胜感激。
<template>
<div id="hello">
<div>
跨域后返回的数据:
{{responseData}}
</div>
</div>
</template>
<script>
import $ from 'jquery'
export default {
name: 'HelloWorld',
data() {
return{
responseData:'hhh'
}
},
mounted:function(){
$.ajax({
url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su',
type: 'get',
dataType: "jsonp",
jsonp: 'cb',
jsonpCallback: 'gg',
data: {'wd': 'dd'},
success: function(res) {
console.log(res)
// 由于jsonp是异步的,所以没有办法把返回值赋给全局变量
this.responseData=res.q
console.log("this.responseData:",this.responseData)
}
});
}
}
</script>