Cross domain Ajax using Json

Normally Ajax should be used within the domain for some security reasons. The XMLHttpRequest will not allow to call from external domains. However whenever if we need to get some date from external domains we use the Ajax with Json

function xss_ajax(url) {
var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', url);
script.setAttribute('id', 'script_id');
script_id = document.getElementById('script_id');
if(script_id){
document.getElementsByTagName('head')[0].removeChild(script_id);
}
document.getElementsByTagName('head')[0].appendChild(script);
}

function callback(data) {
var txt = '';
for(var key in data) {
txt += key + " = " + data[key];
txt += "\n";
}
alert(txt);
}

Call the function by passing the url of the php page
var the_url = "http://yourdomain.com/test/test_page.php";
xss_ajax(the_url);

Create the test_page.php

$obj = array();
$obj['firstname'] = "John";
$obj['lastname'] = "Smith";
$obj['email'] = "john.smith@johnsmith.com";
$response = "callback(" . json_encode($obj) . ");";
print $response;

The callback data should be created as valid xml format to send multiple lines of data. If you want to send single line of data you modify the callback function as follows
$response = "callback('test line of data');";

modify the javascript callback as
function callback(data) {
alert(
data);
}