原因:在 then的内部不能使用Vue的实例化的this, 因为在内部 this 没有被绑定
解决方法有:
(1)用ES6箭头函数,箭头方法可以和父方法共享变量
(2)在请求ajax外面定义一下 var that=this
new Vue({
el: "#app",
data: {
sites: [],
},
created: function () {
//为了在内部函数能使用外部函数的this对象,要给它赋值了一个名叫self的变量。
var self = this;
$.ajax({
url: 'http://localhost:8080/student/json',
type: 'get',
dataType: 'json'
}).then(function (res) {
//把从json获取的数据赋值给数组
self.sites=res;
console.log(getQueryVariable("page"))
console.log(res.length)
}).fail(function () {
console.log('失败');
})
}
})