Can I use Javascript to get data from other sites?
Is it possible for a web page using Javascript to get data from another site? In my case, I want to get it for calculations and chart plots. But I'm not sure if this is possible or not due to security concerns. If it is considered not, but there is work to do, I would appreciate it if I could be told about it. I don't want to collect this information on the server side, if possible.
Any help is greatly appreciated.
source to share
Learn about JSONP format and cross-site requests ( http://en.wikipedia.org/wiki/JSON#JSONP ).
You might need to use a "PHP proxy" script on your server side that will fetch information from websites and serve it to your Javascript.
source to share
You cannot fetch data from another server due to the same origin policy. You can do some tricks to get around this, such as placing the URL in a tag <script>
, but in your case it won't work for simple HTML parsing.
Use simple_dom_html to parse your data server side. it's much easier than doing it in JavaScript anyway.
An easy way you could do this is by using an inline iframe . If the web page you're getting data from doesn't have headers, or you can isolate the data you're pulling in (like an image or SWF), this might work.
source to share
jsonp changes this value completely, it allows javascript to be requested from another server (if it has an API that supports jsonp, many large web videos like google, twitter, yahoo, ... do) by specifying a callback function in your code that needs to be run to act on the response.
the javascript response will contain:
- callback function call
- actual payload as a javascript object.
frameworks like jquery offer lightweight jsonp support out of the box .
Once you have the raw data, you can bind google chart tools to create charts on the fly and embed them in your webapp.
source to share
Also worth considering is support for XMLHttpRequest Access Control , which is supported in some modern browsers.
If this service provider you are trying to access via a webpage is configured this way, it is a very simple XMLHttpRequest call and you will access the resources on that site without the need for JSONP (especially useful for requests that are not GETs. i.e. POST, HEAD, etc.)
source to share