Ajax异步提交造成变量undefined

在使用jQuery的get方法或post方法向后台发ajax请求时,在其中定义一个变量htmlcollectionlst,但是在循环结束后却发现是undifined

 $.get("GetPlantDetailHandler.ashx", { lname: json[0].LName }, function (data) {
var htmlcollectionlst = "";
for (var i = 0; i < json.length; i++) {
htmlcollectionlst += json[i].CollectionID;
}
$("#winCollectionList").html(htmlcollectionlst);
});

解决方法:使用ajax方法,设置提交方式为同步

$.ajax({
url: "GetPlantDetailHandler.ashx",
type: "GET",
async: false,
dataType: "JSON",
data: { lname: json[0].LName },
success: function (data, textStatus, jqXHR) {
var htmlcollectionlst = "";
for (var i = 0; i < json.length; i++) {
htmlcollectionlst += json[i].CollectionID;
}
$("#winCollectionList").html(htmlcollectionlst);
}
});
上一篇:scrum学习笔记


下一篇:read()、write()返回 Input/output error, Device or resource busy解决