Cross-origin resource sharing (CORS) is a mechanism that allows many resources (e.g., fonts, JavaScript, etc.) on a web page to be requested from another domain outside the domain from which the resource originated.
Normally Browser does not allow cross domain AJAX requests due to security issues. Cross-domain requests are allowed only if the server specifies same origin security policy.
To enable Cross Domain Request, You need to specify below HTTP headers in the server using PHP.
Access-Control-Allow-Origin – Name of the domain allowed for cross domain requests or use * for all domains.
Access-Control-Allow-Methods – List of HTTP methods can be used during request (GET, POST, …).
Access-Control-Allow-Headers – List of HTTP headers can be used during request (Content-Type, Content-Range, Content-Disposition, Content-Description).
Ajax:
[sourcecode]
var loo = 1;
var request = $.ajax({
type : ‘POST’,
url : ‘ajax-request.php’,
dataType : ‘json’,
cache: false,
crossDomain:true,
context: document.body,
data: {
postfid:loo
},
success : function(followdata){
alert(followdata.followvalue);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr.status);
alert(thrownError);
}
});
[/sourcecode]
PHP:
[sourcecode]
header(‘Access-Control-Allow-Origin: *’);
header(‘Access-Control-Allow-Methods: GET, POST’);
$followdata[‘followvalue’]= $_POST[‘postfid’];
echo json_encode($followdata);
exit();
[/sourcecode]