1

I'm trying to read this link.

http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1

with this method:

$.getJSON(" http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1",function (json) {
    alert(json.length);
});

But it doesn't work, and I don't know the reason :(

Some one can help me?

[edit]

I just want to read, the "json.length" its was just a test. I tried this with another .json, and worked. I want to know if a need to use some method that read without problems.

Joao Henrique
  • 33
  • 1
  • 9

1 Answers1

1

If the variable fetched is an object, instead of array, you need to use Object.keys(), which gives you the list of keys as an array and you can use the .length on it:

$.getJSON("http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1", function(json) {
  alert(Object.keys(json).length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

One thing I noticed is, the following URL:

http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1

Doesn't have the CORS enabled. So, if you try accessing it, it throws an error in the Console, which is kinda silent. If you mean that, you need to request or make the JSON to have CORS Enabled.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252