Javascript 即使响应正确,jQuery ajax调用后数据也不会更新
我有下面的代码Javascript 即使响应正确,jQuery ajax调用后数据也不会更新,javascript,jquery,ajax,Javascript,Jquery,Ajax,我有下面的代码 var utility = function() { this.table = null; this.parsedData = null; } utility.prototype.fetchData = function(url, query) { var self = this; var params = null; if (query) params = query;
var utility = function() {
this.table = null;
this.parsedData = null;
}
utility.prototype.fetchData = function(url, query) {
var self = this;
var params = null;
if (query)
params = query;
$.ajax({
url: url,
data: params
})
.done(function(resp) {
resp = JSON.parse(resp);
if (resp.resp === 200) {
self.parsedData = resp.data;
}
});
return this;
};
我使用下面的事件处理程序调用它
var utilityObj = new utility();
$('#section').on('change', function() {
var self = this;
$.ajax({
url: 'api/get_tests.php',
method: 'GET',
data: {
section_id: this.value
}
}).done(function(resp) {
/* some other function calls here for dom manipulation */
utilityObj.fetchData('api/get_sections_tasks.php', {
section_id: self.value
});
});
});
在运行fetchData方法时,我发现parsedData仍然为null,并且我还不能确定我的代码中是否有错误。任何帮助都将不胜感激。谢谢
这是在fetchData上收到的响应,它是正确的
{
"resp": 200,
"data": [{
"Id": 1,
"user_id": 1,
"section_id": 1,
"img": "\/uploads\/112015\/0b3eb1cc2a3473453b2be00091fc9a2f.jpg",
"title": "0",
"description": "dsafds",
"workflow": "Doing",
"created": "0000-00-00 00:00:00",
"updated": "0000-00-00 00:00:00"
}, {
"Id": 2,
"user_id": 1,
"section_id": 1,
"img": "\/uploads\/112015\/8bf91b2aa69222d8a337b98b67a486d0.jpg",
"title": "0",
"description": "sdafsdfa",
"workflow": "Backlog",
"created": "0000-00-00 00:00:00",
"updated": "0000-00-00 00:00:00"
}]
}
在这段代码中
var utilityObj = new utility();
$('#section').on('change', function() {
var self = this;
$.ajax({
url: 'api/get_tests.php',
method: 'GET',
data: {
section_id: this.value
}
}).done(function(resp) {
/* some other function calls here for dom manipulation */
utilityObj.fetchData('api/get_sections_tasks.php', {
section_id: self.value
});
});
});
在此之前尝试console.log(self.value)
utilityObj.fetchData('api/get_sections_tasks.php', {
section_id: self.value
});
我认为这就是问题所在。但是我不确定。fetchData方法中的“resp”是什么?如果您在问题上加上“resp”值就好了。我提到ajax调用都返回了正确的数据,问题在于fetchData()方法的异步性质。这就是我还没弄明白的