views:

232

answers:

5

Is it possible for a web page using Javascript to get data from another website? In my case I want to get it for calculations and graphing a chart. But I'm not sure if this is possible or not due to security concerns. If it is considered a no no but there is a work around I would appreciate being told the work around. I don't want to have to gather this information on the server side if possible.

Any and all help is appreciated.

+2  A: 

Learn about JSONP format and cross-site requests (http://en.wikipedia.org/wiki/JSON#JSONP).

You may need to use the "PHP-proxy" script at your server side which will get the information from the websites and provide it to yours Javascript.

Sergey Kuznetsov
A: 

You can't pull data from another server due to the same origin policy. You can do some tricks to get around it, such as putting the URL in a <script> tag, but in your case it wouldn't work for just parsing HTML.

Use simple_dom_html, to parse your data server side. it is much easier than doing it in JavaScript anyways.

A simple way you might be able to do this is to use an inline iframe. If the web page you are getting the data from has no headers, or you can isolate the data being pulled in (to say an image or SWF), this might work.

Byron Whitlock
If you can't pull data from another server using Javascript, how does DoubleClick stays in business?
Robusto
@Robusto, The same way Google ads does it. They host a JavaScript file that you pull into your page. This JavaScript dynamically writes the the ad in a div on your page for you. Either that or you write the iframe manually. remote javascript source + Iframe != pulling data from another page.
Byron Whitlock
And is that not pulling data from another server?
Robusto
@Robusto, No because it is writing an iframe. an iframe is in essense another browser window. It's javascript doesn't run in the same scope as the javascript in the main frame.
Byron Whitlock
+1  A: 

The only reliable way is to let "your" webserver act as a proxy. In PHP you can use curl() to fire a HTTP request to an external site and then just echo the response.

BalusC
A: 

cross-domain javascript used to be impossible, using a (php-)proxy was a workaround for that.

jsonp changes this entirely, it allows to request javascript from another server (if it has an API that supports jsonp, a lot of the bigger webplayers like google, twitter, yahoo, ... do), specifying the callback-function in your code that needs to be triggered to act on the response.

the response in javascript will contain:

  • a call to a callback-function you defined
  • the actual payload as a javascript-object.

frameworks like jquery offer easy support for jsonp out of the box.

once you have the raw data you could tie into google chart tools to create graphs on the fly and insert them in your webapp.

futtta
A: 

Also worth considering is support for XMLHttpRequest Access Control which is support in some modern browsers.

If the service provider that you are trying to access via a web page has this set up, it is a very simple call to XMLHttpRequest and you will get access to the resources on that site without the need for JSONP (especially useful for requests that are not GET, i.e. POST, HEAD etc)

Kinlan