这是我返回的JSON字符串的第一部分(来自firebug中的POST响应)
{"d":"{\"companies\" : [{\"CompanyID\" : \"5\",\"CompanyName\" : \"(No Insurance Carrier)\"},{\"CompanyID\" : \"0\",\"CompanyName\" : \"(None Specified)\"},{\"CompanyID\" : \"72431\",\"CompanyName\" : \"A.M. Technologies, Inc./W.P. Hickman Systems\"},{\"CompanyID\" : \"72486\",\"CompanyName\" : \"AAA\"},...]}"}
这是pageLoad的脚本:
$.ajax({
type: "POST",
url: strURL + "/GetCompanyNames",
contentType: "application/json; charset=utf-8",
data: "{'strApplicationName':'hidden', 'strAPIkey':'hidden'}",
dataType: "json",
success: function(msg) {
var options = $("#ddlCompany").attr('options');
$('option', $("#ddlCompany")).remove();
$("#ddlCompany").get(0).options[0] = new Option("Select Company", "-1");
$.each(msg.d, function(item) {
options[options.length] = new Option(item.CompanyName, item.CompanyID);
});
},
error: function() {
alert("Failed to load Companies");
}
});
我得到的错误是CompanyName未定义。谁能看到我在这里做错了什么?我是否需要先在msg.d中找到表格?
$.ajax({
type: "POST",
url: strURL + "/GetCompanyNames",
contentType: "application/json; charset=utf-8",
data: "{'strApplicationName':'hidden', 'strAPIkey':'hidden'}",
dataType: "json",
success: function(msg) {
$("#ddlCompany").get(0).options.length = 0;
$("#ddlCompany").get(0).options[0] = new Option("Select Company", "-1");
window.alert(msg.d);
for (var i = 0; i <= msg.d.companies.length - 1; i++) {
$("#ddlCompany").get(0).options[$("#ddlCompany").get(0).options.length] = new Option(msg.d.companies[i].CompanyName, msg.d.companies[i].CompanyID);
}
},
error: function() {
alert("Failed to load Companies");
}
});
我也试过这个,但找不到公司。窗口警报完美地显示了msg.d JSON。
所以这不应该是必要的,但如果我将结果解析为JSON,它可以在Firefox中运行,但不能在IE中运行吗?例如:
$.ajax({
type: "POST",
url: strURL + "/GetCompanyNames",
contentType: "application/json; charset=utf-8",
data: "{'strApplicationName':'hidden', 'strAPIkey':'hidden'}",
dataType: "json",
success: function(msg) {
if (msg.hasOwnProperty("d")) { msg = msg.d; }
var json = JSON.parse(msg);
$("#ddlCompany").get(0).options.length = 0;
$("#ddlCompany").get(0).options[0] = new Option("Select Company", "-1");
for (var i = 0; i <= json.companies.length - 1; i++) {
$("#ddlCompany").get(0).options[$("#ddlCompany").get(0).options.length] = new Option(json.companies[i].CompanyName, json.companies[i].CompanyID);
}
},
error: function() {
alert("Failed to load Companies");
}
});
为什么这个脚本会在IE中挂起?
答案 0 :(得分:0)
为什么msg.d
? d
在哪里?据我所知,它应该是companies
。并且该值作为第二个参数传递给回调,请参阅documentation:
$.each(msg.companies, function(index, item) {
options[options.length] = new Option(item.CompanyName, item.CompanyID);
});