Got it solved! was a really bad combination: forEach did not work but $.each did - but none of them did work locally in IE (in FF it did) because I fetched the json via cross-side scripting which has to happen (according to IE8 standards) with the same protocol. locally it was file:// and http:// uploaded to my webspace the two matched.
Javascript for-loop on json in IE8
-
14-07-2023 - |
Pergunta
I am receiving some json looking like this (which is in the following stored in the var response):
[{"complexity":"1","name":"Model1"}
,{"complexity":"2","name":"Model2"}]
to iterate it and to fill the names in a list within css ul tags named with the class .model
<ul class="model"></ul>
I'm using a simple foreach-loop. Which seeems to be totally ok in Firefox and IE9 (and greater). But in IE it's not working. (working in FF but not IE8)
function(response) {
response.forEach(function (model) {
$('.model').append('<li>' + model['name'] +'</li>');
});
That is why I tried the following options using jQuery v1.8.2 with no success: (working in FF but not IE8)
jQuery.each(response, function(index, model){
$('.model').append('<li>' + model['name'] +'</li>');
});
Also using "map" did not do the trick (working in FF but not IE8)
var a = jQuery.map(response, function(model){
$('.model').append('<li>' + model['name'] +'</li>');
});
According to my opinion the easiest way (and also most straight-forward) would be using a for-loop which is (working in none of the two: neither FF nor IE8)
var model;
for(var i = 0; i < response.length; i++) {
model = reponse[i];
$('.model').append('<li>' + model['name'] +'</li>');
});
also not working
for(var model in response) {
$('.model').append('<li>' + model['name'] +'</li>');
});
Anyone any ideas?
Solução
Outras dicas
Your JSON looks like an array of objects. Use model.name instead of model[name]