I know about AJAX cross-domain policy. So I can't just call "http://www.google.com" over a ajax HTTP request and display the results somewhere on my site.
I tried it with dataType "jsonp", that actually would work, but I get a syntax error (obviously because the received data is not JSON formated)
Is there any other possiblity to receive/display data from a foreign domain? iFrames follow the same policy?
The only (easy) way to get cross-domain data using AJAX is to use a server side language as the proxy as Andy E noted. Here's a small sample how to implement that using jQuery:
The jQuery part:
And the PHP (proxy.php):
Simple as that. Just be aware of what you can or cannot do with the scraped data.
You will need to dynamically insert a script tag into the page that references the data. Using JSONP, you can execute some callback function when the script has loaded.
The wikipedia page on JSONP has a concise example; the script tag:
would return the JSON data wrapped in a call to
parseResponse
:(depending on the configuration of the
getjson
script on domain1.com)The code to insert the tag dynamically would be something like:
I faced the same problem during 2 days and I found the solution, and it's elegant after googling a lot. I needed xss Ajax for some widget clients which pull datastream from tiers websites to my Rails app. here's how I did.
If you are using a php script to get the answer from the remote server, add this line at the begining:
I use this code for cross domain ajax call, I hope it will help more than one here. I'm using Prototype library and you can do the same with JQuery or Dojo or anything else:
Step 1: create a new js file and put this class inside, I called it xss_ajax.js
This class creates a dynamic script element which src attributes targets your JSON data provider (JSON-P in fact as your distant server must provide the data in this format :: call_back_function(//json_data_here) :: so when the script tag is created your JSON will be directly evaled as a function (we'll talk about passing the callback method name to server on step 2), the main concept behind this is that script like img elements are not concerned by the SOP constraints.
Step2: in any html page where you wanna pull the JSON asynchronously (we call this AJAJ ~ Asynchronous JAvascript + JSON :-) instead of AJAX which use the XHTTPRequest object) do like below
D'you remenber the callback on step 1? so we pass it to the server and it will returns the JSON embeded in that method so in our case the server will return an evalable javascript code xss_crawler.process(//the_json_data), remember that xss_crawler is an instance of WSAjax class. The server code depends on you (if it's yours), but most of Ajax data providers let you specify the callback method in parameters like we did. In Ruby on rails I just did
and that's all, you can now pull data from another domain from your apps (widgets, maps etc), in JSON format only, don't forget.
I hope it was helpfull, thanks for your patience :-), peace and sorry for code formatting, it doesn't work well
Here is an easy way of how you can do it, without having to use anything fancy, or even JSON.
First, create a server side script to handle your requests. Something like http://www.example.com/path/handler.php
You will call it with parameters, like this: .../handler.php?param1=12345¶m2=67890
Inside it, after processing the recieved data, output:
Now, in the client side script, use the following:
The only limit of this approach, is the max length of parameters that you can send to the server. But, you can always send multiple requests.