0

I want to post data to a controller in CakePHP, but posting with JQuery always results in"POST http//localhost/SA/myController/editUserData/1 400 (Bad Request)" error and I can't figure out why.

In my view I have the following method, that posts the data to the controller page

   $scope.saveUser = function() {
    $.ajax({
        type: 'POST',
        url: '<?php echo Router::url(array(
                                        'controller' => 'myController',
                                        'action' => 'editUserData',
                                        0 => $userInfo['user']['id'],));?>',
        data: { email: 'cabraham@delhi.k12'},//"my edited data for example"
        success: function (data) {
          alert(data);
        }
    });

My controller method looks like this:

  public function editUserData($id) {
       if($this->request->is('post') || $this->request->is('put')) {
           $this->AcsaUser->save($this->request->data('email'));//edit and save the new data
           echo 'ok';
       }
   }

Any ideas??

Dana
  • 43
  • 7
  • What a strange key '0' for router. Why not 'id'? And set dataType attribute to ajax object. – Alex Nov 30 '14 at 13:13
  • @Alex Not _that_ strange, but unnecessary, it will work fine without explicitly specifying a key, using a named key like `id` however won't work as this would indicate a named element, this wouldn't match possible routes and generate URLs with something like `/id:1`. – ndm Nov 30 '14 at 19:06
  • possible duplicate of [saving data from jquery](http://stackoverflow.com/questions/18422675/saving-data-from-jquery) – ndm Nov 30 '14 at 19:08

1 Answers1

0

Two things that may be throwing it.

(1) Cake's built-in security - so exempt the method from it:

In AppController.php

public function beforeFilter() {
    $this->Security->unlockedActions = array('editUserData')
}

(2) Decide how you want your editUserData method to render the view, if you're echo'ing out an 'ok' it will still pull in the default layout and look for an editUserData.ctp in the view (and cause an error if it doesn't find the file), so

To not render anything, ie a .ctp view file and the default layout.. in the editUserData($id), add

$this->autoRender = false;

To only render the view file and not the layout:

$this->autoLayout = false;

......Then lastly I wound just add this parameter in the ajax call :

dataType : 'html' // (or JSON?)
david.philip
  • 420
  • 2
  • 13
  • Thanks @david.philip, the problem was in the layout and I solved it as you suggested in point 2, but this line $this->AcsaUser->save($this->request->data('email')); still not working, do you have any idea? or how shall I edit my data? – Dana Dec 02 '14 at 07:34
  • Glad you came right! So, assuming the 'AcsaUser' Model is being loaded in myController, set the model ID for updating with: *$this->AcsaUser->id = $id;* then save it as you have it, also in your ajax, it's not wrong how you have it but it's a bit neater to have it as => *url : 'Html->url(array('controller' => 'myController', 'action' => 'editUserData',$id)); ?>';* – david.philip Dec 02 '14 at 09:39
  • Yes, thank you :) The problem was with setting the id. This line solved the issue $this->AcsaUser->id = $id; – Dana Dec 02 '14 at 19:09