0
votes

I have been trying to send some data to a controller via AJAX but for the life of me I can`t seem to make it work; everytime I make the request, a 403 forbidden error is thrown.

this is the ajax request:

$.ajax({
            type: 'post',
            url:"<?php echo Router::url(array('controller'=>'Atls','action'=>'saveTime', '_ext' => 'json'));?>",
            dataType: 'json',
            data: {atl_id: idTimerPaused, time: actual_time},
            beforeSend: function(xhr){

            },
            success: function (response) {

                console.log('Nailed It');

            },
            error: function(jqXHR, exception){
                console.log(jqXHR);
            }
});
return false;

the controller action:

public function saveTime()
{

    if ($this->request->is('post') && $this->request->is('ajax')) {
        $content = $this->request->getData();

            $query = $this->Atls->query();
            $result = $query
                ->update()
                ->set(
                    $query->newExpr('actual_time = '. $content['time'])
                )
                ->where([
                    'id' => $content['atl_id']
                ])
            ->execute();

        $this->set(compact('content'));
        $this->set('_serialize', ['content']);

        $this->render('ajax_response', 'ajax');
    }
}

I have loaded the extensions on the routes.php file (Router::extensions('json', 'xml');)

The request handler is also loaded and the function is allowed:

public function initialize()
{
    parent::initialize();
    $this->loadComponent('RequestHandler');
}

public function beforeFilter(Event $event)
{
    parent::beforeFilter($event);
    $this->Auth->allow('saveTime');

    //Change layout for Ajax requests
    $this->viewBuilder()->layout('appTemplate');
    if ($this->request->is('ajax')) {
       $this->viewBuilder()->layout('ajax');
    }
}

that "ajax_response" view has also been added.

I can't see where the problem could be. So any help I can get to work this out would be much appreciated.

2
hi do you have any .htacess and an URL rewriting enabled on your server?user8556290
@headmax the .htaccess is the generic one that comes with the framework, It has not been modified. I have mod_rewrite enabled.DanielHolguin
any folder right to limited ? inferior to 755 in the process call? any error from your apache or php logs ?user8556290
that's what frustrate me the most, no errors are shown on the logs and all the folders are 755.DanielHolguin
take a look too the rights of the file calling if is 644 :( ?user8556290

2 Answers

0
votes

When you got an 403 forbidden error in most cases the session is expired and the user has to login again.