Bitcoin Forum

Economy => Service Discussion => Topic started by: tenbatsu on June 11, 2011, 06:52:53 AM



Title: JQuery and mtgox ticker not playing nicely
Post by: tenbatsu on June 11, 2011, 06:52:53 AM
Any thoughts on why this fails?

Code:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function() {
  $.ajax({
    url: 'https://mtgox.com/code/data/ticker.php',
    dataType: 'jsonp',
    success: function(data){
      // Note: because of failure, not even trying to print out the ticker data at this point
      alert('foo');
    }
  });
});
</script>
</head>
<body>
</body>
</html>

In firefox 4.0.1, I get this error:

Code:
Error: invalid label
Source File: https://mtgox.com/code/data/ticker.php?callback=jQuery1610765425372311095_1307774805819&_=1307774805835
Line: 1, Column: 1
Source Code:
{"ticker":{"high":28.601,"low":20.01,"vol":98283,"buy":21.751,"sell":21.8156,"last":21.8158}}

In chrome 12.0.742.91, I get these errors:

Code:
ticker.php: -1Resource interpreted as Script but transferred with MIME type text/html.
ticker.php: 1Uncaught SyntaxError: Unexpected token :


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: Maged on June 11, 2011, 07:33:31 AM
dataType: 'jsonp'

The dataType should just be 'json', since that's all your getting back. Besides, unless you control the server giving you the data, you shouldn't use jsonp.


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: tenbatsu on June 11, 2011, 07:39:35 AM
But it -is- cross domain, so json isn't an option, right?

Firefox gives neither an error nor the javascript alert and chrome fails as expected with the following:

Code:
XMLHttpRequest cannot load https://mtgox.com/code/data/ticker.php. Origin http://[___MY_SITE___] is not allowed by Access-Control-Allow-Origin.


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: Maged on June 11, 2011, 08:03:29 AM
But it -is- cross domain, so json isn't an option, right?

Firefox gives neither an error nor the javascript alert and chrome fails as expected with the following:

Code:
XMLHttpRequest cannot load https://mtgox.com/code/data/ticker.php. Origin http://[___MY_SITE___] is not allowed by Access-Control-Allow-Origin.
That actually depends on the site. That said, it appears that MtGox supports neither CORS or JSONP. You'll have to ask him about it.

In the mean time, that means that you'll have to have your server grab the data and act as a proxy.


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: tenbatsu on June 11, 2011, 08:06:50 AM
Quote
In the mean time, that means that you'll have to have your server grab the data and act as a proxy.

Darn, I was hoping to avoid that.  Ah well.

Thank you for your help!


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: lego on June 11, 2011, 08:43:48 AM
Your best bet would be to cache the ticker to your server at certain intervals and query that as json. :-\


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: eturnerx on June 11, 2011, 10:15:03 AM
I had a similar problem - so had to create a proxy script to run on my server. Pretty easy to do actually.


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: tenbatsu on June 11, 2011, 10:18:55 AM
Easy, yes, but I have neither a reliable server nor a reliable IP.  I was hoping to rely on mtgox.


Title: Re: JQuery and mtgox ticker not playing nicely
Post by: eturnerx on June 11, 2011, 02:56:19 PM
MTGox has a websockets interface that you might be able to use instead.
http://websocket.org/ (http://websocket.org/) - THe official website
http://caniuse.com/#feat=websockets (http://caniuse.com/#feat=websockets) - Lookup browser support



Title: Re: JQuery and mtgox ticker not playing nicely
Post by: molecular on June 12, 2011, 10:26:00 AM
MTGox has a websockets interface that you might be able to use instead.
http://websocket.org/ (http://websocket.org/) - THe official website
http://caniuse.com/#feat=websockets (http://caniuse.com/#feat=websockets) - Lookup browser support



here's the forum topic with lots of info (also on the currently existing bugs):

http://forum.bitcoin.org/index.php?topic=5855.0