Ajax with PHP
Building a basic REST API. For example to be used by third-party service.
I used that code to interface with the builtin REST API caller from Mobile framework Sencha Touch.
Allowing other domains (cross-domain) to access your REST API.
This is done with the header() below. It will be read by the browser and the browser will allow the AJAX HTTP call to proceed. Note that it should not be considered a security feature because anyone from anywhere will be able to access it without problem from any domain using a non-browser method - for example via a custom script. More on breaking the cross-domain barrier and discussion about security.
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, OPTIONS, POST, GET");
$ajax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
//if ($ajax)
{
header("Content-Type: application/json");
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS')
{
exit;
}
Recent Comments