I am trying to retrieve the values that I pass through an array in json to ajax.
this is the code i use:
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' }); }
});
}
and this is the array that json returns:
[{"idean":"1316","nombre":"843446300058"}]
The thing is that I would like to assign those received values to different inputs but I don't know why it gives me an undefined result.
What I can be doing wrong? or how to retrieve the values in ajax?
if it returns the result that way enclosed within
[...]
means that it is an array in this case you have an array of objects inside an array so I think you could, first get the position of the array and then the objects it containshere an example:
Cheers
The data you receive is an array; it will suffice that you indicate the index that you wish to access; if it is a single element:
Using JsonEditorOnline the Json looks like this:
So you must access as follows: