jQuery Question

I can not access data from a JSON object

I have a problem I can not access the data of a JSON object, it returns Object Object when using "each"

function NumeroConos(id_cono) {
var ParamObjSend = {
'id_cono' :id_cono,
};

$.ajax({
type: "POST",
url: "<?= base_url() ?>AgregarOTController/NumeroConos",
data: ParamObjSend,
dataType: 'json',
success: function(objView) {
var items = objView.NumeroConos[0].numero_conos.split(';');

$.each(items, function (ind, elem) {
var option = document.createElement('option');
option.value = $(this);
option.textContent = $(this);
$('#numero_conos').append(option);
});
}
});
}


output - Object Object

JSON

{
"success": true,
"NumeroConos": [
{
"id_cono": "1",
"descripcion_cono": "sdfasdf",
"color_cono": "Rojo",
"numero_conos": "1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50",
"token": "GcKegUhEh7kQAsf35fefkPmBMhwyKQvBFBcJ1W5z720xk9uegy",
"estado": "0"
}
]
}

Answer Source

The solution, suggested by ajai Jothi in the comment, solves your issue.

Instead, if you want to take advantage of how jQuery Creates New Elements on the fly you may write in a reduced format:

$.each(items, function (ind, elem) {
    $('#numero_conos').append($('<option/>', {value: elem, text: elem}));
});

The example:

var objView = {"success":true,"NumeroConos":[{"id_cono":"1","descripcion_cono":"sdfasdf","color_cono":"Rojo","numero_conos":"1;2;3;4;5;6;7;8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32;33;34;35;36;37;38;39;40;41;42;43;44;45;46;47;48;49;50","token":"GcKegUhEh7kQAsf35fefkPmBMhwyKQvBFBcJ1W5z720xk9uegy","estado":"0"}]};


var items = objView.NumeroConos[0].numero_conos.split(';');

$.each(items, function (ind, elem) {
    $('#numero_conos').append($('<option/>', {value: elem, text: elem}));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


<select id="numero_conos">
</select>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download