Bitcoin Forum

Bitcoin => Project Development => Topic started by: RGBKey on March 09, 2014, 01:57:51 AM



Title: Help with Javascript and Github Pages
Post by: RGBKey on March 09, 2014, 01:57:51 AM
Hello, I was trying to make a site that would display API data from multiple exchanges and display it to the user as a learning project but when I went to actually use javascript to get the API data, my IP was blocked from Bitstamp's servers. I assume this is because someone on github pages went over the request limit and got the IP banned. Is there a way to make the javascript send the request from the client IP instead of the server so that the IP isn't blocked? Thanks!


Title: Re: Help with Javascript and Github Pages
Post by: 9inety7even on March 09, 2014, 02:41:37 AM
If you're using something like jQuery.get() then it's already sending it from the client IP.


Title: Re: Help with Javascript and Github Pages
Post by: scribe on March 09, 2014, 08:20:52 AM
Are you authenticating with the bitstamp API properly? Not used it myself but the doc page says all requests need to be authenticated.  Or are you seeing a message specifically relating to limits?


Title: Re: Help with Javascript and Github Pages
Post by: RGBKey on March 09, 2014, 11:10:09 PM
Are you authenticating with the bitstamp API properly? Not used it myself but the doc page says all requests need to be authenticated.  Or are you seeing a message specifically relating to limits?
I'm using the public part (https://www.bitstamp.net/api/ticker/) so it doesn't need authed per the docs. On my webpage with jQuery, I opened the console and did this:
Code:
var test = jQuery.getJSON("https://www.bitstamp.net/api/ticker/");
And got the error
Code:
XMLHttpRequest cannot load https://www.bitstamp.net/api/ticker/. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://rgbkey.github.io' is therefore not allowed access.

I would appreciate if anyone could tell me why this doesn't work. I can load the page fine from my web browser.


Title: Re: Help with Javascript and Github Pages
Post by: 9inety7even on March 09, 2014, 11:49:04 PM
Like the error says, you're trying to make a cross-domain request, which it looks like bitstamp doesn't allow. You have a few options here, but I think the best one would just be to proxy the request through your servers. You can find some more info and code for doing that in PHP at these links:

http://stackoverflow.com/questions/19821753/jquery-xml-error-no-access-control-allow-origin-header-is-present-on-the-req
http://stackoverflow.com/questions/12683530/origin-http-localhost-is-not-allowed-by-access-control-allow-origin


Title: Re: Help with Javascript and Github Pages
Post by: gogodr on March 10, 2014, 04:54:02 AM
I use a work around for your problem here:
http://btca.frikicorp.com/
I use whateverorigin, it bypasses the cross domain control with a clever trick (setting up a local proxy)
I use the btc-e api here, but you can get the idea

$.getJSON('http://whateverorigin.org/get?url=https://btc-e.com/api/2/btc_usd/ticker&callback=?', function(data){
       BTCBuy = JSON.parse(data.contents).ticker.buy;
       BTCSell = JSON.parse(data.contents).ticker.sell;
      });


Title: Re: Help with Javascript and Github Pages
Post by: olsn on March 10, 2014, 09:34:28 PM
I'd go with gogodr's solution, it'll be the simplest for you - BUT ONLY IF you just request public ticker data, you should in NO CASE send private user-data (passwords ect...) through 3rd-party proxy-services.
If you want to read more about JSONP and how to evade the cross-domain issue for JSON-requests, you can take a look here: http://en.wikipedia.org/wiki/JSONP (or google for JSONP)


Title: Re: Help with Javascript and Github Pages
Post by: RGBKey on March 11, 2014, 12:59:07 AM
I'd go with gogodr's solution, it'll be the simplest for you - BUT ONLY IF you just request public ticker data, you should in NO CASE send private user-data (passwords ect...) through 3rd-party proxy-services.
If you want to read more about JSONP and how to evade the cross-domain issue for JSON-requests, you can take a look here: http://en.wikipedia.org/wiki/JSONP (or google for JSONP)
Thanks to gogodr, that should work just fine, and of course I'll only be using it for non-private information.


Title: Re: Help with Javascript and Github Pages
Post by: Zickafa on March 11, 2014, 06:43:32 PM
If you whant to make a site that would display API data from multiple exchanges and display it to the user use php better!


Title: Re: Help with Javascript and Github Pages
Post by: 9inety7even on March 11, 2014, 08:54:20 PM
I'd go with gogodr's solution, it'll be the simplest for you - BUT ONLY IF you just request public ticker data, you should in NO CASE send private user-data (passwords ect...) through 3rd-party proxy-services.
If you want to read more about JSONP and how to evade the cross-domain issue for JSON-requests, you can take a look here: http://en.wikipedia.org/wiki/JSONP (or google for JSONP)
Thanks to gogodr, that should work just fine, and of course I'll only be using it for non-private information.

The only other thing to be careful of is that you're now putting yourself in the hands of whateverorigin: they could send back false or even malicious data. Not that I have any reason to suspect that they would, but it's just something to think about.

If you whant to make a site that would display API data from multiple exchanges and display it to the user use php better!

He could still be using PHP; he just wants the client to load the information instead of doing it on the server.


Title: Re: Help with Javascript and Github Pages
Post by: gogodr on March 11, 2014, 11:07:31 PM
I'd go with gogodr's solution, it'll be the simplest for you - BUT ONLY IF you just request public ticker data, you should in NO CASE send private user-data (passwords ect...) through 3rd-party proxy-services.
If you want to read more about JSONP and how to evade the cross-domain issue for JSON-requests, you can take a look here: http://en.wikipedia.org/wiki/JSONP (or google for JSONP)
Thanks to gogodr, that should work just fine, and of course I'll only be using it for non-private information.

The only other thing to be careful of is that you're now putting yourself in the hands of whateverorigin: they could send back false or even malicious data. Not that I have any reason to suspect that they would, but it's just something to think about.

If you whant to make a site that would display API data from multiple exchanges and display it to the user use php better!

He could still be using PHP; he just wants the client to load the information instead of doing it on the server.
if that ever were to happen, they have their whole project open sourced. Anyone can take the source and make his own whateverorigin.
There are a couple of limitations though. you cant POST or GET parameters using whateverorigin.


Title: Re: Help with Javascript and Github Pages
Post by: RGBKey on March 12, 2014, 12:37:25 AM
I'd go with gogodr's solution, it'll be the simplest for you - BUT ONLY IF you just request public ticker data, you should in NO CASE send private user-data (passwords ect...) through 3rd-party proxy-services.
If you want to read more about JSONP and how to evade the cross-domain issue for JSON-requests, you can take a look here: http://en.wikipedia.org/wiki/JSONP (or google for JSONP)
Thanks to gogodr, that should work just fine, and of course I'll only be using it for non-private information.

The only other thing to be careful of is that you're now putting yourself in the hands of whateverorigin: they could send back false or even malicious data. Not that I have any reason to suspect that they would, but it's just something to think about.

If you whant to make a site that would display API data from multiple exchanges and display it to the user use php better!

He could still be using PHP; he just wants the client to load the information instead of doing it on the server.
I really want to do as much as I can in javascript and will only resort to PHP as a last resort. Also, a MITM is no problem because all i'm fetching is ticker data. They can't really do anything with that and nothing important is going to be stored on the site anyways.