通过 JSON 和 jQuery 到 Selectbox 的对象数组

发布于 2024-11-08 15:11:09 字数 816 浏览 0 评论 0原文

我在将数据集(对象数组)从 servlet 传输到 jsp/jquery 时遇到问题。

这是 servlet (Json) 发送的数据集:

[
    {aktion:"ac1", id:"26"},
    {aktion:"ac2", id:"1"},
    {aktion:"ac3", id:"16"},
    {aktion:"ac4", id:"2"}
] 

jsp:

function getSelectContent($selectID) {
alert('test');
 $.ajax({
        url:'ShowOverviewDOC',
        type:'GET',
        data: 'q=getAktionenAsDropdown',
        dataType: 'json',
        error: function() {
            alert('Error loading json data!');
        },

    success: function(json){
        var output = '';
        for (p in json) {

    $('#'+$selectID).append($('<option>').text(json[p].aktion).attr('value', json[p].aktion));

}
                    }});

};

如果我尝试运行此错误(“加载 json 数据时出错”),则会发出警报。 有人知道错误可能出在哪里吗? 谢谢!

I have problems transferring a dataset (array of objects) from a servlet to a jsp/jquery.

This is the dataset sent by the servlet (Json):

[
    {aktion:"ac1", id:"26"},
    {aktion:"ac2", id:"1"},
    {aktion:"ac3", id:"16"},
    {aktion:"ac4", id:"2"}
] 

The jsp:

function getSelectContent($selectID) {
alert('test');
 $.ajax({
        url:'ShowOverviewDOC',
        type:'GET',
        data: 'q=getAktionenAsDropdown',
        dataType: 'json',
        error: function() {
            alert('Error loading json data!');
        },

    success: function(json){
        var output = '';
        for (p in json) {

    $('#'+$selectID).append($('<option>').text(json[p].aktion).attr('value', json[p].aktion));

}
                    }});

};

If I try to run this the Error ('Error loading json data') is alerted.
Has someone an idea where the mistake may be?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

滿滿的愛 2024-11-15 15:11:09

如果 error 函数正在运行,则您的服务器将返回错误响应(HTTP 响应代码 >= 400)。

If the error function is running, then your server is returning an error response (HTTP response code >= 400).

记忆消瘦 2024-11-15 15:11:09

要准确了解发生了什么情况,请检查错误回调提供的 textStatus 和 errorThrown 信息。这可能有助于缩小范围。

http://api.jquery.com/jQuery.ajax/

To see exactly what is going on, check the textStatus and errorThrown information that is provided by the error callback. That might help narrow it down.

http://api.jquery.com/jQuery.ajax/

赤濁 2024-11-15 15:11:09

您设置数据参数的方式看起来有点可疑(请注意下面示例中的 JSON 编码)。下面是调用 .Net asmx 的样子。

$.ajax({
  url: "/_layouts/DashboardService.asmx/MinimizeWidgetState",
  data: "{'widgetType':'" + widgetType + "', 'isMinimized':'" + collapsed + "'}"
});

此外,返回数据默认放置在返回变量的 .d 属性中。您可以通过添加一些 ajax 设置脚本来更改此默认行为。

//Global AJAX Setup, sets default properties for ajax calls. Allows browsers to make use of native JSON parsing (if present)
//and resolves issues with certain ASP.NET AJAX services pulling data from the ".d" attribute.
$.ajaxSetup({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: "{}",
    success: function(msg) {
        if (this.console && typeof console.log != "undefined")
            console.log(msg);
    },
    dataFilter: function(data) {
        var msg;

        //If there's native JSON parsing then use it.
        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
            msg = JSON.parse(data);
        else
            msg = eval('(' + data + ')');

        //If the data is stuck in the "."d" property then go find it.
        if (msg.hasOwnProperty('d'))
            return msg.d;
        else
            return msg;
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        handleAjaxError(XMLHttpRequest, textStatus, errorThrown);
    }
});

The way you are setting the data parameter looks a bit suspect (notice JSON encoding in my example below). Here is how it would look calling a .Net asmx

$.ajax({
  url: "/_layouts/DashboardService.asmx/MinimizeWidgetState",
  data: "{'widgetType':'" + widgetType + "', 'isMinimized':'" + collapsed + "'}"
});

Also the return data is by default placed in the .d property of the return variable. You can change this default behavior by adding some ajax setup script.

//Global AJAX Setup, sets default properties for ajax calls. Allows browsers to make use of native JSON parsing (if present)
//and resolves issues with certain ASP.NET AJAX services pulling data from the ".d" attribute.
$.ajaxSetup({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    data: "{}",
    success: function(msg) {
        if (this.console && typeof console.log != "undefined")
            console.log(msg);
    },
    dataFilter: function(data) {
        var msg;

        //If there's native JSON parsing then use it.
        if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function')
            msg = JSON.parse(data);
        else
            msg = eval('(' + data + ')');

        //If the data is stuck in the "."d" property then go find it.
        if (msg.hasOwnProperty('d'))
            return msg.d;
        else
            return msg;
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        handleAjaxError(XMLHttpRequest, textStatus, errorThrown);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文