Jquery Php Cors Example

Post ajax request is not possible with Cross Domain if you don’t use CORS.

Here is an example for Cors.

[js]
jQuery.ajax({
type: "POST",
url: "http://example.com/user.php",
data: {"name" : "john"},
dataType: "json",
success: function(e){
console.log(e);
}
});
[/js]

This is PHP part – user.php

[php]
header(‘HTTP/1.0 200’);
header(‘Access-Control-Allow-Methods: "PUT, POST, GET, OPTIONS, DELETE"’);
header(‘Access-Control-Allow-Origin: *’);

print_r($_POST);
[/php]

You need to define headers. If you don’t define it, you get Server Internal Error (500).