我想补充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');
...
尝试改变这一行:
$('<div class="prueba"/><br>')+(response[i].name).appendTo('#filaNombre');
为此:
$('<div class="prueba"><br>' + response[i].name + '</div>').appendTo('#filaNombre');
forEach
response.forEach((item)=> {
$('#filaNombre').append(`<div class="prueba">${item.name}</div>`);
});
allUsers()
is used to populate JSON
responsefunction 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>`);
}
}
});
}