Bitcoin Forum
May 04, 2024, 06:31:41 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Blockchain AJAX Request  (Read 2111 times)
StewieG (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile
September 13, 2013, 12:05:47 AM
 #1

Hi Guys,

I'm trying to do a request on the blockchain.info api using ajax. for some reason I am getting a response with no data in it. I'm trying the same on MtGox and it works. I think it has to do with the "same Origin" policy in ajax. can somebody help me with this issue or knows a good workaround?
If necessary I can post the code here...
1714804301
Hero Member
*
Offline Offline

Posts: 1714804301

View Profile Personal Message (Offline)

Ignore
1714804301
Reply with quote  #2

1714804301
Report to moderator
1714804301
Hero Member
*
Offline Offline

Posts: 1714804301

View Profile Personal Message (Offline)

Ignore
1714804301
Reply with quote  #2

1714804301
Report to moderator
Make sure you back up your wallet regularly! Unlike a bank account, nobody can help you if you lose access to your BTC.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
StewieG (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile
September 13, 2013, 03:29:10 AM
 #2

Meeeh, I was trying to avoid that. It is much more work... I would need to set database entries and look when the data was last checked so I don´t send data to blockchain.info everytime a user clicks on my site... A nice little javascript would let the user check the current values. I think that would be way better concerning live data and also my server wouldn´t need to check the data, which could cause the site to load slower when the script gets triggered. But I guess you also don´t have a solution to my javascript problem?(if there is any)
Maybe you know if it is Blockchain.info that does not want to respond to javascripts or is it the javascript that does not want to connenct to blockchain.
I analyzed the traffic in firefox and it shows that I´m actually sending the data and getting a response, but the response are only headers and no data...
So if this "same Origin" policy is there for security reasons like preventing XSS, then it is not doing its job, right? Cuz it sends data to the server anyway. I have not tested how far this policy goes and if it is possible to send cookies or something like that.
Also it works for MtGox to receive data from the server. If the address is like "http://something.something.com/xyz"(like it is when using the gox api) it seems to work. If the address is like "http://something.com/xyz" it does not work? Also I can get data from Blockchain.info when I open the html file with the script in it with the Internet Explorer from a file(file://xyz.html), but when I do it with a server running(localhost/xyz.html) it does not work... In Firefox it doesn´t work at all. WTF?! I couldn´t find a solution to this problem, but I´m sure I´m not the first one that has this problem. Any help is appreciated. This stuff is really driving me mad!!!  Angry
rampantparanoia
Sr. Member
****
Offline Offline

Activity: 516
Merit: 283



View Profile
September 13, 2013, 03:42:04 AM
 #3

you need a php script that does a simple curl request towards blockchain api. Something like this:

Code:
<?php
$url 
"http://whateverapi.com/you/want/to/query.php";
$ch curl_init($url);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_BINARYTRANSFERtrue);
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
curl_setopt($chCURLOPT_HTTPHEADER, array('Host: whateverapi.com'));
$output curl_exec($ch);
curl_close($ch);
echo 
$output;
?>


then you make an ajax call to that script, and make sure they're on the same domain to avoid xss. be sure to include any auth tokens that are required by the api

i have this exact setup running on my server for my own personal ticker. ajax request to local php script which queries an API, updating ticker values.

goes without saying, you need curl support on your server.
StewieG (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile
September 13, 2013, 03:56:48 AM
 #4

you need a php script that does a simple curl request towards GOX api. Something like this:

Code:
<?php
$url 
"http://whateverapi.com/you/want/to/query.php";
$ch curl_init($url);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_BINARYTRANSFERtrue);
curl_setopt($chCURLOPT_SSL_VERIFYPEERFALSE);
curl_setopt($chCURLOPT_HTTPHEADER, array('Host: whateverapi.com'));
$output curl_exec($ch);
curl_close($ch);
echo 
$output;
?>


then you make an ajax call to that script, and make sure they're on the same domain to avoid xss.

i have this exact setup running on my server here for my own personal ticker: http://samad64.com/coin/

goes without saying, you need curl support on your server.

I appreciate your help buddy, but that is exactly what I want to prevent. If I do it this way my server will make that query everytime somebody triggers the script. I think my IP would be blocked by MtGox in notime if I would do it this way. I would need to check when it received data the last time, so it would connect to blockchain like at max every 10 or 15 seconds. Instead I want the user to connect to blockchain, which can be done pretty easy in javascript.(at leat I thought) I really don´t want to use php for this one. I want rather understand why I can get data with AJAX from MtGox and why it fails to get data from blockchain.
rampantparanoia
Sr. Member
****
Offline Offline

Activity: 516
Merit: 283



View Profile
September 13, 2013, 04:39:20 AM
 #5

the server is probably configured to allow xss, whereas blockchain isnt. its a configuration in apache/nginx/whatever http server being used
demonmaestro
Hero Member
*****
Offline Offline

Activity: 574
Merit: 500


Mining for the hell of it.


View Profile
September 13, 2013, 05:50:27 AM
 #6

mtgox has a api already out.

Feel Like Donating? bc1q0v5nfdejapffewu67gft7zw7zsmnfmmkt3lf02
Buy/Sell BitCoin & LiteCoin  Click here! | Looking for a great exchange? CoinBase Has you covered.
StewieG (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile
September 13, 2013, 02:12:06 PM
 #7

the server is probably configured to allow xss, whereas blockchain isnt. its a configuration in apache/nginx/whatever http server being used
Okay so as far as I understand, blockchain has this very sweet api where I can get all kind of information, but I am not allowed to use it in javascript, right?
rampantparanoia
Sr. Member
****
Offline Offline

Activity: 516
Merit: 283



View Profile
September 13, 2013, 02:14:29 PM
 #8

the server is probably configured to allow xss, whereas blockchain isnt. its a configuration in apache/nginx/whatever http server being used
Okay so as far as I understand, blockchain has this very sweet api where I can get all kind of information, but I am not allowed to use it in javascript, right?

not without a middleman (the php script)
Abdussamad
Legendary
*
Offline Offline

Activity: 3612
Merit: 1564



View Profile
September 14, 2013, 12:36:06 AM
 #9

I appreciate your help buddy, but that is exactly what I want to prevent. If I do it this way my server will make that query everytime somebody triggers the script.

No. You split up the data fetching php script and the ajax query handling PHP script. The data fetching part gets executed every x minutes via a cron job. It saves the data to the database. The ajax query handling part merely reads the data from the db and relays it back to the client. This is how I do it on my site.
StewieG (OP)
Hero Member
*****
Offline Offline

Activity: 532
Merit: 500



View Profile
September 15, 2013, 01:09:39 AM
 #10

Okay I understand thanks for the help guys. ^^
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!