4

I am trying to access data using getJSON function of jquery but not able to succeed.

This is my js file:

$(document).ready(function(){
  $("button").click(function(){

    $.getJSON( "https://www.bitstamp.net/api/eur_usd/", function( data ) {
    alert(data);
    });
  });
});

Can anyone help me in getting this work?

Will really appreciate.

user2206724
  • 1,265
  • 3
  • 20
  • 38
  • 2
    I assume that is an external domain to the one making the request in which case this will not work due to the [Same Origin Policy](http://en.wikipedia.org/wiki/Same-origin_policy). You need to check if the API you're querying accepts JSONP requests, or is setup for CORS. – Rory McCrossan Nov 14 '13 at 09:57
  • possible duplicate of [jQuery getJSON works locally, but not cross domain](http://stackoverflow.com/questions/6849802/jquery-getjson-works-locally-but-not-cross-domain) – Liam Nov 14 '13 at 10:01
  • Try using console.log(data.responseData); to view any errors – Pete Thorne Nov 14 '13 at 10:02
  • check this out http://stackoverflow.com/questions/19456146/ajax-call-and-clean-json-but-syntax-error-missing-before-statement – rajesh kakawat Nov 14 '13 at 10:07

2 Answers2

10

try to have error message in ajax call

$(document).ready(function() {
        $("button").click(function() {

            $.getJSON("https://www.bitstamp.net/api/eur_usd/", function(data) {
                    alert(data);
                }
                .error(function(xhr) {
                    alert(xhr)
                })
            );
        });
    });

Note: Xhr.responseText will provide you the stack trace of error, it returns in the html format

Krishna
  • 101
  • 4
0

Need to use JSON.stringify(data) instead of just data

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109