FOR with getJSON file URL

1

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

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>
    
asked by anonymous 20.12.2015 / 21:40

1 answer

3

You must pass a callback to .getJSON to be able to return the data. You can have this for same cycle but it has to be inside the callback, because .getJSON is asynchronous and will pass the JSON that you want only for the callback.

The code looks 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 must remove the domain and use only /appOperacoes.php . This way you prevent the ajax request from being interpreted as "from another domain" and blocked by CORS .

    
20.12.2015 / 22:19