我最近在javascript工作,我在ajax调用中遇到问题。我初始化的变量是未定义的,在这种情况下是鳕鱼。我不明白真正的问题在哪里。我正在写一段代码,我得到了错误。如果有人能提供帮助,我将不胜感激。
Cod.prototype.getAll = function (successCallback, failureCallback) {
var service = REST_API_BASE_URL + "/cods";
var self = this;
$.ajax({
url: service,
type: "GET",
dataType: "json",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
Authorization: 'Bearer ' + self.token
},
data: {},
success: function (response) {
var objsCods = [];
var cods = response.cods;
if (cods.length> 0){
var numCods = cods.length;
}
for (var i = 0; i < numCods; ++i) {
var objCods = new Cod();
objCods.setID(cods[i].ID);
objCods.setCodId(cods[i].codId);
objCods.setLdv(cods[i].ldv);
objCods.setAmount(cods[i].amount);
objCods.setCustomerId(cods[i].customerId);
objCods.setCustomerCode(cods[i].customerCode);
objCods.setChannel(cods[i].channel);
objsCods.push(objCods);
}
if (typeof successCallback === "function")
successCallback(objsCods);
},
error: function (xhr, textStatus, errorThrown) {
if (typeof failureCallback === "function") {
var retObj = null;
try {
retObj = JSON.parse(xhr.responseText);
} catch (e) {
retObj = {status: {code: xhr.statusCode, message: xhr.responseText || textStatus}};
}
failureCallback(retObj);
}
}
});
};
后端部分的一切工作正常。请帮我!提前谢谢!