user2206724
user2206724

Reputation: 1365

jQuery not working for getJSON

I am trying to use Bitstamp API. But somehow it is not working.

This is the code:

$.getJSON("https://www.bitstamp.net/api/ticker/", function(person){

$.each(person, function(key, value){
    document.write(key+":"+value+"<br />"); 
});

});

And here is jsfiddle for it: http://jsfiddle.net/mojit/QKTrD/

I am not getting what is wrong here. As when I run the API url on browser it works. But it is nor working when I try to access its parameter using jQuery.

I tried to replace the url with other's API url like MtGox and MtGox works perfectly. But Bitstamp's doesn't.

Can anybody tell me whats going wrong?

Will really appreciate.

Thanks.

Upvotes: 2

Views: 256

Answers (1)

Krasimir
Krasimir

Reputation: 13529

You are trying to access a data from another domain. It is called cross-domain request and it is not normally allowed. There is a technique called jsonp and you should check if this is supported by bitstamp. If not, then you should make a proxy. By proxy I mean some local file which uses some server side technology to make get requests. You may implement this in php, nodejs or whatever you are using. And then your js code will make request to a file on your server.

$.ajax({
  dataType: "json",
  url: "proxy.php",
  data: { url: "https://www.bitstamp.net/api/ticker/" },
  success: function(result) {
    $.each(person, function(key, value){
      // ...
   });
  }
});

So, just pass the url which you want to fetch data from.

Upvotes: 2

Related Questions