I have some troubles with ajax cross-scripting request using jquery. I'm in a subdomain named test.example.com and i'm doing a ajax call to www.example.com/action like this :
$.ajax({
url:"http://www.example.com/action",
type:"post",
crossDomain:true,
dataType:"jsonp",
jsonp:"cross_domain_action",
xhrFields:{withCredentials:true},
success:function(data){}
});
The problem is that i dont know how to test if the request is an ajax one or not in www.example.com/action.
I'm using CakePHP 1.3 and there is a component call "request_handler.php" with the function
function isAjax() {
return env('HTTP_X_REQUESTED_WITH') === "XMLHttpRequest";
}
I found that there is not that HTTP header in the request.
X-Requested-With XMLHttpRequest
If you have any ideas :) Thanks
Well that's exactly what you use to test if it's and ajax request. I usually use:
if(isset($_SERVER['HTTP_X_REQUESTED_WITH'])
&& strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
{//this is an ajax request}
You don't need to manually check the headers. Cake 1.3 provides this information in the controller's $this->params
property:
$this->params['isAjax']
will store 1 if the current request is an ajax call and 0 if not. Check it here. .