简体   繁体   English

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

[英]Ajax request check is not working in AppController Cakephp

The ajax request not detected in the AppController. 在AppController中未检测到ajax请求。

I have printed the params array. 我已经打印了params数组。 Here is the sample. 这是示例。

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] => 
)

This is how I have checked the ajax request.. 这就是我检查ajax请求的方式。

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

But always I get false result ig not ajax request. 但总是我得到错误的结果ig不是ajax请求。

I am using the cakephp 2.3 version. 我正在使用cakephp 2.3版本。

Here is the code sample of the ajax request code. 这是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);
                   }
              }
        });

In your ajax request(jquery script) you have added context: this . 在您的ajax请求(jquery脚本)中,您添加了上下文:this

Can you please try to remove this and then check in backend for the ajax. 您能否尝试删除它,然后在后端检查Ajax。

Try $this->RequestHandler->isAjax() . 尝试$this->RequestHandler->isAjax()

If return true your request is ajax. 如果返回true,则您的请求是ajax。 But before add 'RequestHandler' component in your controller. 但在控制器中添加“ RequestHandler”组件之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM