</div>
</div>
<div class="grid--cell mb0 mt4">
<a href="/questions/733314/jquery-loop-over-json-result-from-ajax-success" dir="ltr">jQuery loop over JSON result from AJAX Success?</a>
<span class="question-originals-answer-count">
(12 answers)
</span>
</div>
<div class="grid--cell mb0 mt8">Closed <span title="2019-02-13 11:13:50Z" class="relativetime">last year</span>.</div>
</div>
</aside>
I return data from PHP Like that.
$return['fillable'] = [
'field_one',
'field_two',
'field_three',
'field_four',
'field_five',
];
$json = json_encode($return);
return $json;
Now in my ajax response, I want to loop fillable array.
$.ajax({
'type': "GET",
'global': false,
'dataType': 'json',
'url': "/commission-process/"+$('#token').val(),
'data': {'ajax': true},
'success': function (data) {
// how to loop my return array data.
}
});
My Question is that, I want to loop fillable array which prints like that.
field_one
field_two
field_three
field_four
field_five
Thanks.
</div>
Since I didn't know whether you wanted to loop through it in PHP or Javascript, here's a result in both.
PHP: You have to first decode the json like this:
$array = json_decode( $json, true );
Then you can loop through it like through any other array.
Javascript:
for(i = 0; i<data.fillable.length; i++)
{
console.log(data.fillable[i]);
}
$.ajax({
'type': "GET",
'global': false,
'dataType': 'json',
'url': "/commission-process/"+$('#token').val(),
'data': {'ajax': true},
'success': function (data) {
var json = JSON.parse(data);
$.each(json, function(i,e){
console.log(i);
console.log(e);
});
}
});