Cross-domain ajax request
Normally, if you send an ajax request to a server script stays in a different domain server, you will get this error:
XMLHttpRequest cannot load http://my.external.domain/myscript.php. Origin http://my.external.domain is not allowed by Access-Control-Allow-Origin.
So, to allow other domains call this cript, just set the 'access-control-allow-origin' header equal '*'.
For example:
http://my.external.domain/myscript.php
<?php header('content-type: application/json; charset=utf-8');
header('access-control-allow-origin: *');
$date = date('m-d-Y h:i:s a', time());
$json = json_encode($date);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
?>
And in my current site, send the ajax request to myscript.php:
References:
http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
XMLHttpRequest cannot load http://my.external.domain/myscript.php. Origin http://my.external.domain is not allowed by Access-Control-Allow-Origin.
So, to allow other domains call this cript, just set the 'access-control-allow-origin' header equal '*'.
For example:
http://my.external.domain/myscript.php
<?php header('content-type: application/json; charset=utf-8');
header('access-control-allow-origin: *');
$date = date('m-d-Y h:i:s a', time());
$json = json_encode($date);
echo isset($_GET['callback'])
? "{$_GET['callback']}($json)"
: $json;
?>
And in my current site, send the ajax request to myscript.php:
var mydate = '';
jQuery.ajax({
url: 'http://my.external.domain/myscript.php',
crossDomain: true,
type: 'GET'
}).done(function(res){
console.log(res);
mydate = res;
});
References:
http://www.geekality.net/2010/06/27/php-how-to-easily-provide-json-and-jsonp/
Comments
Post a Comment