Cake php 2x未检测到ajax请求


Cake php 2x is not detecting ajax request

我在一个服务器中有一个api,在另一个服务器上有视图文件。当我使用ajax请求时,的值

$this->request->is('ajax')

总是显示错误。但它在本地xampp服务器上运行良好。我还为请求服务器启用了原点。我该怎么办?是因为跨域吗。

我的ajax是

$.ajax(
            {
                url:'http://12.34.567.890/Users/getu.json',
                // url:'<?php echo URL; ?>Users/getu.json',
                type:"POST",
                data:{access_token:'<?php echo $_SESSION["token"]->access_token; ?>', api_key:"***************"},
                async:false,
                success:function(res)
                {
                    console.log(res);
                }
            });

然而,我发现主要的问题是,当在实时服务器中ajax请求没有标题时

X-Requested-With:XMLHttpRequest;

因此,cake无法识别ajax请求。

您的问题可能在ajax调用中。当您进行请求时,您使用的dataType是什么如果您需要进行跨域ajax调用,您必须在ajax调用中使用JSONP作为dataType,如下所示:

$.ajax( {
   url: 'http://my_remote_server/controller/action',
   dataType: 'jsonp',
   success: function( res ) {
      // do stuff with res
      alert("Success");
   },
   error:function() {
      alert("Error");
   }
} );

然后在控制器->操作中,使用以下操作来检测ajax请求:

if( $this->request->is( 'ajax' ) ) {
   // process the request and send output
}