Ajax请求检查在AppController Cakephp中不起作用

时间:2014-02-19 13:12:26

标签: php jquery ajax cakephp

AppController中未检测到ajax请求。

我打印了params数组。这是样本。

CakeRequest Object
(
    [params] => Array
        (
            [plugin] => 
            [controller] => permissions
            [action] => api_auth
            [named] => Array
                (
                )

            [pass] => Array
                (
                )

            [prefix] => api
            [api] => 1
            [ext] => json
            [_Token] => Array
                (
                    [key] => 00f665112046ea0d5794b5f93b6a035f62a59ada
                    [unlockedFields] => Array
                        (
                        )

                )

            [isAjax] => 
        )

    [data] => Array
        (
        )

    [query] => Array
        (
        )

    [url] => api/permissions/auth.json
    [base] => 
    [webroot] => /
    [here] => /api/permissions/auth.json
    [_detectors:protected] => Array
        (
            [get] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => GET
                )

            [post] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => POST
                )

            [put] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => PUT
                )

            [delete] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => DELETE
                )

            [head] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => HEAD
                )

            [options] => Array
                (
                    [env] => REQUEST_METHOD
                    [value] => OPTIONS
                )

            [ssl] => Array
                (
                    [env] => HTTPS
                    [value] => 1
                )

            [ajax] => Array
                (
                    [env] => HTTP_X_REQUESTED_WITH
                    [value] => XMLHttpRequest
                )

            [flash] => Array
                (
                    [env] => HTTP_USER_AGENT
                    [pattern] => /^(Shockwave|Adobe) Flash/
                )

            [mobile] => Array
                (
                    [env] => HTTP_USER_AGENT
                    [options] => Array
                        (
                            [0] => Android
                            [1] => AvantGo
                            [2] => BlackBerry
                            [3] => DoCoMo
                            [4] => Fennec
                            [5] => iPod
                            [6] => iPhone
                            [7] => iPad
                            [8] => J2ME
                            [9] => MIDP
                            [10] => NetFront
                            [11] => Nokia
                            [12] => Opera Mini
                            [13] => Opera Mobi
                            [14] => PalmOS
                            [15] => PalmSource
                            [16] => portalmmm
                            [17] => Plucker
                            [18] => ReqwirelessWeb
                            [19] => SonyEricsson
                            [20] => Symbian
                            [21] => UP\.Browser
                            [22] => webOS
                            [23] => Windows CE
                            [24] => Windows Phone OS
                            [25] => Xiino
                        )

                )

            [requested] => Array
                (
                    [param] => requested
                    [value] => 1
                )

        )

    [_input:protected] => 
)

这是我检查ajax请求的方式..

if($this->request->is('ajax')){
      //ajax request
} else {
     // not ajax request.
}

但我总是得到错误的结果,而不是ajax请求。

我正在使用cakephp 2.3版本。

以下是ajax请求代码的代码示例。

$.ajax({
        url: backendAPI +'/students/profile.json',
        type: 'POST',                     
        data: this.studentdata,
        dataType: 'json',
        context: this
      })
      .done(function(response, textStatus, XMLHttpRequest) {
               if( response.students.status == 'success' ) {
                  self.model.fetch();
                  mStitute.moduleTRANS['LeftSideBarStudent'].fetch();
               } else {
                   if( response.students.message_type == 'single' ) {
                       Core.showAlert(response.students.message.toString(), 'show', response.students.status);
                       Core.hideAlert(10000);
                   } else {
                       var message = "";
                       $.each(response.students.message, function(i, v){
                             message = message + '<br/>' +  v;
                       });
                       Core.showAlert(message, 'show', response.students.status);
                       Core.hideAlert(10000);
                   }
              }
        });

2 个答案:

答案 0 :(得分:1)

在您的ajax请求(jquery脚本)中,您添加了上下文:此

你可以尝试删除它,然后检查后端是否有ajax。

答案 1 :(得分:0)

试试$this->RequestHandler->isAjax()

如果返回true,则您的请求是ajax。但在控制器中添加“RequestHandler”组件之前。