-1

I am working on get json from server in javascript.

i used pure javascript, jquery but i am getting status 0.

$(document).ready(function() {
    $("#btn").click(function(event){
        $.getJSON('http://myhost/myapp/data.json', function(jd) {
            alert(jd);
        });
    });
});

<body>
    <input type="button" id="btn" value="Load Data" />
</body>

getJSON() block not called.

can anyone help me?

giorgio
  • 10,111
  • 2
  • 28
  • 41
bhautikmewada191
  • 653
  • 1
  • 9
  • 20

2 Answers2

0

Check if you allow CORS on your host or if your response was empty!

You might also try to use $.ajax().

$.ajax("http://myhost/myapp/data.json", function (data) {
    console.log(data);
}
Community
  • 1
  • 1
Clemens Himmer
  • 1,340
  • 2
  • 13
  • 26
-2

use jQuery.getJSON() $.ajax({ dataType: "json", url: url, data: data, success: success }); read more from http://api.jquery.com/jquery.getjson

Patrick Mutwiri
  • 1,301
  • 1
  • 12
  • 23
  • 2
    It's basically the same call. OP is just using a shorthand. If the shorthand fails because the URL is incorrect or the JSON is invalid, this will fail as well. The problem is likely elsewhere. – Jeremy Thille Jan 07 '16 at 16:35
  • give it a try first @JeremyThille – Patrick Mutwiri Jan 07 '16 at 16:38
  • 3
    @PatrickMutwiri, There is no need to try, the page you even link to says **This is a shorthand Ajax function, which is equivalent to** to the code you have supplied, both will do exactly the same thing – Patrick Evans Jan 07 '16 at 16:39