简体   繁体   English

无法使用Ajax Json响应填充表

[英]Unable to populate Table with Ajax Json response

I am sending this valid JSON as a response from my webservice call 我正在发送此有效JSON作为来自Web服务调用的响应

[
    [
        {
            "id": 123,
            "vendorName": "PoppyCounter",
            "item": "Chocltae"
        },
        {
            "id": 1234,
            "vendorName": "PoppyCounter",
            "item": "Chocltae"
        },
        {
            "id": 12345,
            "vendorName": "PoppyCounter",
            "item": "Chocltae"
        },
        {
            "id": 123456,
            "vendorName": "MahalakshmiCounter",
            "item": "Chocltae"
        }
    ]
]

This is my Jquery 这是我的jQuery

$(document).ready(function() {
        $.ajax({
            type: 'GET',
            url: 'http://192.168.2.46:8086/Poller/poll/initial',
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            success: function (response) {
                var trHTML = '';
                $.each(response, function (i, item) {
                    trHTML += '<tr><td>' + item.id + '</td><td>' + item.vendorName + '</td><td>' + item.item + '</td></tr>';
                });
                $('#records_table').append(trHTML);
            },
            error: function (e) {
                $("#divResult").html("WebSerivce unreachable");
            }
        });



    });


<table id="records_table" border='1'>
    <tr>
        <th>Rank</th>
        <th>Content</th>
        <th>UID</th>
    </tr>

With this the result is looking like this in browser 这样,结果在浏览器中看起来像这样

在此处输入图片说明

I am editing my question 我正在编辑我的问题

Edited Part 编辑部分

Due to the cross domain restrictions , i am forming my JSON into jsonp as shown below 由于跨域限制,我将JSON转换为JSONp,如下所示

This is my ajax jquery request 这是我的ajax jquery请求

 $(document).ready(function() {
        $.ajax({
            type: 'GET',
            url: 'http://192.168.2.46:8086/Poller/poll/initial',
            jsonpCallback: 'jsonCallback',
            dataType: 'jsonp',
            jsonp: false,
            success: function (response) {
                var trHTML = '';
                $.each(response, function (i, item) {
                      trHTML += '<tr><td>' + item.id + '</td><td>' + item.vendorName + '</td><td>' + item.item + '</td></tr>';
                });
                $('#records_table').append(trHTML);

                doPoll();
            },
            error: function (e) {
                $("#divResult").html("WebSerivce unreachable");
            }
        });

    });

Due to the jsonCallback , its not forming a valid JSON response and i am getting undefined when forming the table IS there anyway i can from the valid JSON response ?? 由于jsonCallback,它不能形成有效的JSON响应,因此无论如何我都可以从有效的JSON响应中形成表格IS时未得到定义?

jsonCallback([
    [
        {
            "id": 123,
            "vendorName": "PoppyCounter",
            "item": "Chocltae"
        },
        {
            "id": 1234,
            "vendorName": "PoppyCounter",
            "item": "Chocltae"
        },
        {
            "id": 12345,
            "vendorName": "PoppyCounter",
            "item": "Chocltae"
        },
        {
            "id": 123456,
            "vendorName": "MahalakshmiCounter",
            "item": "Chocltae"
        }
    ]
])

You have to have one more $.each() loop: 您还必须再有一个$.each()循环:

$.each(response, function (i, item) {
    $.each(item, function(_, o){
      trHTML += '<tr><td>' + o.id + '</td><td>' + o.vendorName + '</td><td>' + o.item + '</td></tr>';
    });
});

your data is an array in an array, and you consider it only as an array 您的数据是数组中的数组,并且您仅将其视为数组

try to do the same each loop on response[0] for example 例如,尝试对response[0]执行相同的each循环

EDIT 编辑

I'm not sure you can use jquery each on a JSON object, instead you should use : 我不知道,你可以使用jQuery each一个JSON对象,而不是你应该使用:

for(var i in response) {
    response[i]; // do something with it
}

You appear to be having another array in that result. 您似乎在该结果中有另一个数组。 Try to loop one more time, like this for instance: http://jsfiddle.net/yH942/1/ 尝试再循环一次,例如这样: http : //jsfiddle.net/yH942/1/

$.each(jsonResponse, function(key, array) {
$.each(array, function(k, object) {
    // your code goes here
});
});

Parse your object after receiving or use $.getJSON instead of $.ajax. 接收或使用$ .getJSON而不是$ .ajax解析对象。 You are trying to use a string as an object. 您正在尝试使用字符串作为对象。

success: function (response) {
   response = JSON.parse(response);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM