我有一个返回 JS 对象的 AJAX 函数。但是,当我将 var 分配给函数时,我得到了未定义。在 FireBug 中调试,一切都很好,除非我尝试将结果分配给var 测试器
这是我的代码...
function AllBidData(HighestBid,MinimumBid,HighestProxyBid) {
return {
HighestBid: HighestBid,
MinimumBid: MinimumBid,
HighestProxyBid: HighestProxyBid
};
}
function GetAllBidData() {
$.ajax({
type: "POST",
dataType: "json",
url: "/VehicleAuction/GetAllBidData",
data: { AuctionID: AuctionID, UserID: UserID }
}).done(function (msg) {
var BidDate = new AllBidData(msg.HighestBid, msg.MinimumBid, msg.HighestProxyBid);
return BidDate;
});
}
var tester = GetAllBidData();
//why is tester is undefined?
更新和解决方案- 所以这就是我解决这个问题的方法 - 使用延迟对象
function GetAllBidData() {
return $.ajax({
type: "POST",
dataType: "json",
url: "/VehicleAuction/GetAllBidData",
data: { AuctionID: AuctionID, UserID: UserID }
});
}
function AllBidData(data) {
return {
HighestBid: data.HighestBid,
MinimumBid: data.MinimumBid,
HighestProxyBid: data.HighestProxyBid
};
}
var tester = null;
tester = GetAllBidData().done(AllBidData);