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

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

This is how I have checked the ajax request..

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);
                   }
              }
        });

最佳答案

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

您能否尝试删除此内容,然后检查后端的 ajax。

关于php - Ajax 请求检查在 AppController Cakephp 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21881522/

相关文章:

jquery - 为什么 responseText 返回空白?

ruby - 如何在 ruby​​ on rails 中使用 Ajax?

php - 无法在 php 中使用 ffmpeg 生成缩略图

.net - CheckboxList 输入在 IE7/6 中不工作

javascript - 循环动画不起作用

javascript - Grails:多个 onload ajax 调用,但只有一个打印到控制台

php - 使用 file_get_contents() 时出现问题

php - Wordpress 数据库 $wpdb 与 get_option()

php - 从 json_decode 输出中获取返回值

javascript - 基于两个值的数组合并