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

发布于 2024-10-29 02:30:19 字数 3398 浏览 3 评论 0原文

这是返回的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

错爱 2024-11-05 02:30:19

为什么是msg.dd 在哪里?据我所知,应该是公司。该值作为第二个参数传递给回调,请参阅文档

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

Why msg.d? Where is d? As far as I can see, it should be companies. And the value is passed as second argument to the callback, see the documentation:

$.each(msg.companies, function(index, item) {
    options[options.length] = new Option(item.CompanyName, item.CompanyID);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文