Bitcoin Forum
April 19, 2024, 04:33:20 AM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Warning: One or more bitcointalk.org users have reported that they strongly believe that the creator of this topic is a scammer. (Login to see the detailed trust ratings.) While the bitcointalk.org administration does not verify such claims, you should proceed with extreme caution.
Pages: [1]
  Print  
Author Topic: [Need Quick Help] Bittrex API: Jquery  (Read 3479 times)
michaeladair (OP)
Sr. Member
****
Offline Offline

Activity: 448
Merit: 250


I'm a Web Developer: HTML, CSS, PHP, JS.


View Profile
January 06, 2015, 11:03:54 PM
 #1

I have no clue why this code is not working...
the same basic style works on Blockchain.info api.

Any ideas?
Code:
<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>

Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713501200
Hero Member
*
Offline Offline

Posts: 1713501200

View Profile Personal Message (Offline)

Ignore
1713501200
Reply with quote  #2

1713501200
Report to moderator
1713501200
Hero Member
*
Offline Offline

Posts: 1713501200

View Profile Personal Message (Offline)

Ignore
1713501200
Reply with quote  #2

1713501200
Report to moderator
1713501200
Hero Member
*
Offline Offline

Posts: 1713501200

View Profile Personal Message (Offline)

Ignore
1713501200
Reply with quote  #2

1713501200
Report to moderator
Videlicet
Legendary
*
Offline Offline

Activity: 868
Merit: 1058


Creator of Nexus http://nexus.io


View Profile WWW
January 07, 2015, 12:30:08 AM
 #2

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:

Code:
<?php
$ch curl_init();
curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
curl_setopt($chCURLOPT_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->Ask8);
echo "<br>BID: ".number_format($json->result->Bid8);
echo "<br>PRICE: ".number_format($json->result->Last8); 
?>


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:

Code:
<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.php

Hope this helps!
Viz.


[Nexus] Created by Viz. [Videlicet] : "videre licet - it may be seen; evidently; clearly"
michaeladair (OP)
Sr. Member
****
Offline Offline

Activity: 448
Merit: 250


I'm a Web Developer: HTML, CSS, PHP, JS.


View Profile
January 07, 2015, 12:32:57 AM
 #3

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 Offline

Activity: 868
Merit: 1058


Creator of Nexus http://nexus.io


View Profile WWW
January 07, 2015, 12:53:28 AM
Last edit: January 07, 2015, 01:18:46 AM by Videlicet
 #4

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:
Code:
header("Access-Control-Allow-Origin: http://coinshield.io");

To allow any Domain:
Code:
header("Access-Control-Allow-Origin: *");

You can also add this to your .htaccess file on the server hosting curl.php:

For any Domain:
Code:
<ifModule mod_headers.c>
    Header set Access-Control-Allow-Origin: *
</ifModule>

Or for specific domains:
Code:
<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

Code:
<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

Code:
<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 Offline

Activity: 448
Merit: 250


I'm a Web Developer: HTML, CSS, PHP, JS.


View Profile
January 07, 2015, 01:13:57 AM
 #5

Not bad, I'll try it out as soon as possible. Thank you!

Videlicet
Legendary
*
Offline Offline

Activity: 868
Merit: 1058


Creator of Nexus http://nexus.io


View Profile WWW
January 07, 2015, 01:18:06 AM
 #6

Not a problem: There is always a way my friend  Smiley

Take Care,
Viz.

[Nexus] Created by Viz. [Videlicet] : "videre licet - it may be seen; evidently; clearly"
michaeladair (OP)
Sr. Member
****
Offline Offline

Activity: 448
Merit: 250


I'm a Web Developer: HTML, CSS, PHP, JS.


View Profile
January 07, 2015, 02:49:40 PM
 #7

Not a problem: There is always a way my friend  Smiley

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?:
Code:
<div id="price"></div>

+Thanks, I got it working...

Videlicet
Legendary
*
Offline Offline

Activity: 868
Merit: 1058


Creator of Nexus http://nexus.io


View Profile WWW
January 07, 2015, 04:34:04 PM
 #8

Code:
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.

Code:
	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 Offline

Activity: 448
Merit: 250


I'm a Web Developer: HTML, CSS, PHP, JS.


View Profile
January 07, 2015, 04:58:20 PM
 #9

Okay, thanks again! I just hate not knowing how my own code works... I like closure Wink

Videlicet
Legendary
*
Offline Offline

Activity: 868
Merit: 1058


Creator of Nexus http://nexus.io


View Profile WWW
January 07, 2015, 05:13:43 PM
 #10

Okay, thanks again! I just hate not knowing how my own code works... I like closure Wink

I'm the same way - it's a good habit to have  Smiley

Take Care,
Viz.

[Nexus] Created by Viz. [Videlicet] : "videre licet - it may be seen; evidently; clearly"
sherrifshenko
Newbie
*
Offline Offline

Activity: 191
Merit: 0


View Profile
January 07, 2015, 05:46:10 PM
 #11

You can also use YQL as a proxy:

https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20json%20where%20url%20%3D%20'https%3A%2F%2Fbittrex.com%2Fapi%2Fv1.1%2Fpublic%2Fgetticker%3Fmarket%3DBTC-DOGE'&format=json&callback=
pancakesmister
Member
**
Offline Offline

Activity: 73
Merit: 10


View Profile
March 14, 2017, 09:22:47 PM
 #12

could someone be kind enough to renew the code?
perhaps leave your btc address for tips Smiley
ty!
koenisch
Newbie
*
Offline Offline

Activity: 2
Merit: 0


View Profile
June 05, 2017, 03:43:48 PM
 #13

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 Offline

Activity: 1
Merit: 0


View Profile
December 24, 2017, 12:57:50 AM
 #14

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.
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!