使用 Jquery AJAX JSON 附加选择选项时出错
这是返回的 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 中挂起吗?
Here is the first part of my JSON string returned (from POST response in Firebug)
{"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\"},...]}"}
Here is the script for 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");
}
});
The error I get is "CompanyName is undefined". Can anyone see what I am doing wrong here? Do I need to get to the table in the msg.d first?
$.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");
}
});
I tried this as well, but companies is not found. The window alert displays the msg.d JSON perfectly.
so this shouldn't be necessary, but if I parse the result into JSON it works in Firefox, but not in IE? For example:
$.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");
}
});
Any ideas why this script would then hang in IE?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么是
msg.d
?d
在哪里?据我所知,应该是公司
。该值作为第二个参数传递给回调,请参阅文档:Why
msg.d
? Where isd
? As far as I can see, it should becompanies
. And the value is passed as second argument to the callback, see the documentation: