I get undefined repeatedly in a select. I load it this way:
function listarcategorias(){
accion="listadocat";
$.ajax({
type: "POST",
url: "../gestionweb/includes/php/getcategoria.php",
data: {"accion":accion},
error: function(){
alert("error petición ajax");
},
success: function(data){
console.log(data);
for (var i = 0; i < data.length; i++) {
var newRow ="<option id="+data[i].idcategoria+">"+data[i].categoria+"</option>";
$(newRow).appendTo("#cat");
}
}
});}
So I have the select in html:
<label class="col-sm-1 control-label">CATEGORIA</label><select id="cat"></select>
And the console.log returns the array well:
[{"idcategoria":"1","categoria":"Fiambre"},{"idcategoria":"2","categoria":"Quesos"},{"idcategoria":"3","categoria":"general"},{"idcategoria":"4","categoria":"construccion en seco"}]
any help?
Missing an important setting
$.ajax
when working withJSON
Source: http://api.jquery.com/jquery.ajax/
PS: Or as @Pablo Lozano says, you can decode it inside the
success
with aJSON.parse()
Good afternoon dear friends, I also solved this problem thanks to your comments. I leave the code. Any questions, my email is [email protected]