2
votes

The ajax request not detected in the AppController.

I have printed the params array. 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..

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

But always I get false result i.g not ajax request.

I am using the cakephp 2.3 version.

Here is the code sample of the ajax request code.

$.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
Can you tell where in the AppController are you printing the params? And also and example of an ajax request you're doing?Nunser
Yes this params array is printed in the function which going to be call with ajax. The array print I have copied from response of ajax request.user3328211
Can you post js code please?Roberto Maldonado
I have updated the question with the ajax code.user3328211
Do you have Router::parseExtensions('json'); enabled?Roberto Maldonado

2 Answers

1
votes

In your ajax request(jquery script) you have added context: this.

Can you please try to remove this and then check in backend for the ajax.

0
votes

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

If return true your request is ajax. But before add 'RequestHandler' component in your controller.