我正在尝试检索我通过json中的数组传递给ajax的值。
这是我使用的代码:
function CompletarEan(cod) {
$.ajax({
url: "./Scripts/Phps/Autocompletar.php?pag=autocompletar_Ean&term="+cod,
type: "post",
dataType: 'json',
success: function(data) {
$("#InAlbaran_Articulo").val(data.idean);
$("#InAlbaran_Ean").val('');
console.log(data.idean);
},
error: function(xhr, status, error){ $().toastmessage('showToast', { text : 'Error con ean '+ xhr.responseText, sticky : true, type : 'error' }); }
});
}
这是 json 返回的数组:
[{"idean":"1316","nombre":"843446300058"}]
问题是我想将这些接收到的值分配给不同的输入,但我不知道为什么它会给我一个未定义的结果。
我可能做错了什么?或者如何检索ajax中的值?
如果它以包含在其中的方式返回结果,
[...]
则意味着它是一个数组,在这种情况下,你在一个数组中有一个对象数组,所以我认为你可以,首先获取数组的位置,然后获取它包含的对象这里有一个例子:
干杯
您收到的数据是一个数组;您只需指明您希望访问的索引即可;如果它是单个元素:
使用JsonEditorOnline,Json 看起来像这样:
所以你必须访问如下: