如何在Cakephp 2.7中检测跨域ajax请求。
我正在将来自我的一个域的请求发送到另一个域。
jQuery Aajx:来自我的一个域名http:// firstdomain.com
$.ajax({
type: 'post',
url: 'http://anotherdomain.com/users/profile/' + id,
data: formData,
dataType: 'json',
//crossDomain: true,
headers: {'X-Requested-With': 'XMLHttpRequest'}
success: function(result) {
}
});
CakePHP控制器到我的另一个域安装了ssl的https:// anotherdomain.com
App::uses('AppController', 'Controller');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Max-Age: 1000');
header('Access-Control-Allow-Headers: Content-Type');
class UsersController extends AppController {
public $helpers = array('Html', 'Form', 'Session', 'Js', 'Paginator');
public $components = array('Email', 'RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
}
public function profile($id) {
//$this->request->onlyAllow('ajax'); // No direct access via browser URL
$this->layout = false;
$this->autoRender = false;
echo $this->request->is('ajax'); die; //no output here. but $this->request->is('post') true
}
}