FOR with getJSON file URL

Asked

Viewed 76 times

1

I need to make a FOR with getJSON file. As is the correct form?

selectOcorrencias = $.getJSON("http://izicondominios.com.br/appOperacoes.php?operacao=selectOcorrencias&condominioID=2");


for (seOcor in selectOcorrencias){
  document.write(selectOcorrencias[seOcor].ID_Ocorrencia + selectOcorrencias[seOcor].morador + "<br />");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>

1 answer

3


You need to pass a callback to the .getJSON can return the data. You can still have this cycle for but he has to be inside the callback, because the .getJSON is asynchronous and will pass the JSON you want only to callback.

The code could be like this:

$.getJSON("http://izicondominios.com.br/appOperacoes.php", function(selectOcorrencias){
    for (var seOcor in selectOcorrencias){
      document.write(selectOcorrencias[seOcor].ID_Ocorrencia + selectOcorrencias[seOcor].morador + "<br />");
    }
});

Note: If the URL is to be used in the same domain, then you should remove the domain and use only /appOperacoes.php. In this way you prevent the ajax request from being interpreted as "from another realm" and being blocked by CORS.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.