How to read json from url to javascript?

advertisements

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?


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);
}