使用Jquery AJAX JSON附加选择选项时出错

时间:2011-03-30 18:27:05

标签: jquery ajax json select options

这是我返回的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中挂起?

1 个答案:

答案 0 :(得分:0)

为什么msg.dd在哪里?据我所知,它应该是companies。并且该值作为第二个参数传递给回调,请参阅documentation

$.each(msg.companies, function(index, item) {
    options[options.length] = new Option(item.CompanyName, item.CompanyID);
});