开发者

Problem with append json_encode

I want append service by jQuery.each(), but it in my js code not worke开发者_如何学运维d?

This is my output from PHP code:

{
    "data": [{
        "service": ["shalo", "jikh", "gjhd", "saed", "saff", "fcds"],
        "address": "chara bia paeen"
    }, {
        "service": ["koko", "sili", "solo", "lilo"],
        "address": "haminja kilo nab"
    }, {
        "service": ["tv", "wan", "hamam", "kolas"],
        "address": "ok"
    }]
}

This is is my jQuery code:

$.ajax({
    type: "POST",
    dataType: "json",
    url: 'get_residence',
    data: dataString_h,
    cache: false,
    success: function (respond) {
        $.each(respond.data, function (index, value) {
            $('ol li').append('<a href="">' + value.service + '</a>');
        });
    },
    "error": function (x, y, z) {
        alert("An error has occured:\n" + x + "\n" + y + "\n" + z);
    }
});

What do i do?


You will need another $.each loop because service is an Array:

 $.each(respond.data, function (index, value) {
     $.each(value.service, function () {
         $('ol li').append('<a href="">' + this + '</a>');
     });
 });

to just format the array:

 $.each(respond.data, function () {
     $('ol li').append('<a href="">' + this.service.join(', ') + '</a>');
 });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜