开发者

Problem with append json_encode

开发者 https://www.devze.com 2023-04-08 13:31 出处:网络
I want append service by jQuery.each(), but it in my js code not worke开发者_如何学运维d? This is my output from PHP code:

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

精彩评论

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

关注公众号