michaeladair (OP)
Sr. Member
Offline
Activity: 448
Merit: 250
I'm a Web Developer: HTML, CSS, PHP, JS.
|
|
January 06, 2015, 11:03:54 PM |
|
I have no clue why this code is not working... the same basic style works on Blockchain.info api. Any ideas? <html> <head> <title>Michael</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <script> function getPrice(){ $.ajax({ url: "https://bittrex.com/api/v1.1/public/getticker?market=BTC-DOGE", dataType: "text", success: function(data) { var json = $.parseJSON(data); $("#price").html(json.result.Last); setTimeout(function(){getPrice();}, 3000); } }); } $(window).load(function(){ getPrice(); }); </script> </head> <body> <div id="price"></div> </body> </html>
|
|
|
|
Videlicet
Legendary
Offline
Activity: 868
Merit: 1058
Creator of Nexus http://nexus.io
|
|
January 07, 2015, 12:30:08 AM |
|
No need for jQuery - you'll run into a lot of obstacles trying to make cross domain requests with AJAX. This is from old tricks with XSS creating same origin policies which will subject you to the mercy of the web browser. Try this with php and curl to get the ticker data. Name it curl.php on your server: <?php $ch = curl_init(); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL,"https://bittrex.com/api/v1.1/public/getticker?market=BTC-DOGE"); $result = curl_exec($ch); curl_close($ch); $json = json_decode($result); echo "ASK: ".number_format($json->result->Ask, 8); echo "<br>BID: ".number_format($json->result->Bid, 8); echo "<br>PRICE: ".number_format($json->result->Last, 8); ?>
Then for your main page, name it whatever you would like. This will make an AJAX request on an interval loop to your domain to keep domain origin policies for all web browsers: <html> <head> <title>Michael</title> <script type="text/javascript"> function AJAX_REQUEST(strAddress) { document.getElementById("status").innerHTML = "Updating Ticker...."; /** Object to handle XML Requests. */ var ajax_http; try { /** Opera 8.0+, Firefox, Safari **/ ajax_http = new XMLHttpRequest(); } catch (e){ /** Internet Explorer **/ try{ ajax_http = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try{ ajax_http = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e){ return false; } } } /** Asynchronous event on AJAX Completion. */ ajax_http.onreadystatechange = function() { if(ajax_http.readyState != 4) return; if (ajax_http.status == 200) { document.getElementById("price").innerHTML = ajax_http.responseText; document.getElementById("status").innerHTML = "Waiting..."; } else document.getElementById("status").innerHTML = "Failed to Update...."; }
ajax_http.open("GET", strAddress, true); ajax_http.send(); }
setInterval(function(){
AJAX_REQUEST("curl.php"); }, 1000);
</script> </head>
<body> <div id="status"></div> <div id="price"></div> </body> </html>
Here it is running on my server: http://videlicet.io/test.phpHope this helps!Viz.
|
[ Nexus] Created by Viz. [ Videlicet] : "videre licet - it may be seen; evidently; clearly"
|
|
|
michaeladair (OP)
Sr. Member
Offline
Activity: 448
Merit: 250
I'm a Web Developer: HTML, CSS, PHP, JS.
|
|
January 07, 2015, 12:32:57 AM |
|
It helps, but the problem is I'm wanting to make a ticket that people can use on their websites... would this still work? Or would it be too hard on the hosting?
|
|
|
|
Videlicet
Legendary
Offline
Activity: 868
Merit: 1058
Creator of Nexus http://nexus.io
|
|
January 07, 2015, 12:53:28 AM Last edit: January 07, 2015, 01:18:46 AM by Videlicet |
|
Same origin policies are there for a reason, so you'll run into a lot of headaches. Manually you can have this in your curl.php to your server to accept cross domain requests: To allow specific Domains: header("Access-Control-Allow-Origin: http://coinshield.io");
To allow any Domain: header("Access-Control-Allow-Origin: *");
You can also add this to your .htaccess file on the server hosting curl.php: For any Domain: <ifModule mod_headers.c> Header set Access-Control-Allow-Origin: * </ifModule>
Or for specific domains: <ifModule mod_headers.c> Header set Access-Control-Allow-Origin: coinshield.io </ifModule>
And then test.php from coinshield.io: http://coinshield.io/test.php<html> <head> <title>Michael</title> <script type="text/javascript" src="http://videlicet.io/ticker.js"></script> </head>
<body> <div id="status"></div> <div id="price"></div> </body> </html>
User would be able to include your javascript file to view on their site. Last choice is use an iframe: http://coinshield.io/iframe.html<iframe src="http://videlicet.io/test.php"></iframe>
This will maintain your security with same origin policies. Regards,Viz.edit: added a couple more options
|
[ Nexus] Created by Viz. [ Videlicet] : "videre licet - it may be seen; evidently; clearly"
|
|
|
michaeladair (OP)
Sr. Member
Offline
Activity: 448
Merit: 250
I'm a Web Developer: HTML, CSS, PHP, JS.
|
|
January 07, 2015, 01:13:57 AM |
|
Not bad, I'll try it out as soon as possible. Thank you!
|
|
|
|
Videlicet
Legendary
Offline
Activity: 868
Merit: 1058
Creator of Nexus http://nexus.io
|
|
January 07, 2015, 01:18:06 AM |
|
Not a problem: There is always a way my friend Take Care,Viz.
|
[ Nexus] Created by Viz. [ Videlicet] : "videre licet - it may be seen; evidently; clearly"
|
|
|
michaeladair (OP)
Sr. Member
Offline
Activity: 448
Merit: 250
I'm a Web Developer: HTML, CSS, PHP, JS.
|
|
January 07, 2015, 02:49:40 PM |
|
Not a problem: There is always a way my friend Take Care,Viz.Okay, I hate not understanding the code that I use... Where in the javascript does it set the code that displays in here?: +Thanks, I got it working...
|
|
|
|
Videlicet
Legendary
Offline
Activity: 868
Merit: 1058
Creator of Nexus http://nexus.io
|
|
January 07, 2015, 04:34:04 PM |
|
document.getElementById("status").innerHTML = "Updating Ticker...."; This is how you dynamically update the DOM from Javascript. You can change anything from the innerHTML to the styles, ect. Useful for dynamic pages. function() { if(ajax_http.readyState != 4) return; if (ajax_http.status == 200) { document.getElementById("price").innerHTML = ajax_http.responseText; document.getElementById("status").innerHTML = "Waiting..."; } else document.getElementById("status").innerHTML = "Failed to Update...."; }
This is the Async function that sets the div innerHTML from the AJAX Request. Let me know if you need anything else,Viz.
|
[ Nexus] Created by Viz. [ Videlicet] : "videre licet - it may be seen; evidently; clearly"
|
|
|
michaeladair (OP)
Sr. Member
Offline
Activity: 448
Merit: 250
I'm a Web Developer: HTML, CSS, PHP, JS.
|
|
January 07, 2015, 04:58:20 PM |
|
Okay, thanks again! I just hate not knowing how my own code works... I like closure
|
|
|
|
Videlicet
Legendary
Offline
Activity: 868
Merit: 1058
Creator of Nexus http://nexus.io
|
|
January 07, 2015, 05:13:43 PM |
|
Okay, thanks again! I just hate not knowing how my own code works... I like closure I'm the same way - it's a good habit to have Take Care,Viz.
|
[ Nexus] Created by Viz. [ Videlicet] : "videre licet - it may be seen; evidently; clearly"
|
|
|
|
pancakesmister
Member
Offline
Activity: 73
Merit: 10
|
|
March 14, 2017, 09:22:47 PM |
|
could someone be kind enough to renew the code? perhaps leave your btc address for tips ty!
|
|
|
|
koenisch
Newbie
Offline
Activity: 2
Merit: 0
|
|
June 05, 2017, 03:43:48 PM |
|
Hi, i did also have trouble getting this to work but after installing the needed "php5-curl" package and restarting apache everything was running just fine on my bananapi. thought id share this if someone has the same problem.
|
|
|
|
brokenmold
Newbie
Offline
Activity: 1
Merit: 0
|
|
December 24, 2017, 12:57:50 AM |
|
When using js, you can avoid all header issue headache by learning a little node.js. Cors-Anywhere is a pre-fab package I found to be quite useful. That way you can have your own proxy to sort out all cors issues on API calls. If you haven't messed around with node, I highly recommend it in general.
|
|
|
|
|