【发布时间】:2017-08-12 13:54:51
【问题描述】:
这与Initializing Vue data with AJAX有关
我目前使用的是 Vue 2.2.4。我创建了一个 Vue 元素,并在“就绪”块内创建了 ajax 函数,就像上面的示例一样,但没有渲染任何内容。没有打印 console.log,表明这些 ajax 请求甚至没有被调用。有人知道发生了什么吗?假设我必须为此任务使用 jQuery ajax 库。
下面是我的代码:
var newJobsVue = new Vue({
el: '#new-jobs',
data: {
jobs: []
},
methods: {
ready: function () {
var self = this;
return $.ajax({
method: 'GET',
url: 'https://api.indeed.com/ads/apisearch',
crossDomain: true,
dataType: 'jsonp',
data: {
'v': '2',
'format': 'json',
'publisher': <My_Key>,
q: 'javascript',
l: '94112',
userip: 'localhost',
useragent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2)',
latlong: '1'
}
})
.done(function(response){
//render the jobs from the search_response
console.log("response is>>>>>", response.results);
//nope, this is not actually logged
self.jobs = response.results;
})
.fail(function(error){
console.log("error is>>>", error);
// neither is this one logged
});
}
}
});
【问题讨论】: