添加响应AJAX在一个div

问题描述 投票:0回答:2

我想补充JSON格式到列的回应,但我得到以下错误:“遗漏的类型错误:响应[I] .name.appendTo不是一个函数”。

function allUsers(){
        $(".error").remove();
        $("#tabla").remove();
        $.ajax({
            type: "GET",
            url: "mylocalhost/public/api/user",
            contentType: "application/json",    
            dataType:'json',

             beforeSend: function(request) {
                    request.setRequestHeader("Authorization", localStorage.getItem("token"));
                },
            success: function(response){
                console.log(response);




for (i = 0; i < response.length; i++) {

                    $('<div class="prueba"/><br>')+(response[i].name).appendTo('#filaNombre');

                    ...
json ajax laravel append
2个回答
1
投票

尝试改变这一行:

$('<div class="prueba"/><br>')+(response[i].name).appendTo('#filaNombre');

为此:

$('<div class="prueba"><br>' + response[i].name + '</div>').appendTo('#filaNombre');

1
投票

Use below code to append name using forEach

response.forEach((item)=> {
    $('#filaNombre').append(`<div class="prueba">${item.name}</div>`);
});

allUsers() is used to populate JSON response

function allUsers() {
    $(".error").remove();
    $("#tabla").remove();
    $.ajax({
        type: "GET",
        url: "mylocalhost/public/api/user",
        contentType: "application/json",    
        dataType:'json',

         beforeSend: function (request) {
                request.setRequestHeader("Authorization", localStorage.getItem("token"));
            },
        success: function (response) {
          if (response) {
            response.forEach((item)=> {
                $('#filaNombre').append(`<div class="prueba">${item.name}</div>`);
            });
          } else {
            $('#filaNombre').append(`<div class="prueba">No User Found</div>`);
          }
        }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.