0

I am trying to run an ajax request in CakePHP 2.3. I am working off of this post: simple ajax example with cakephp 2.3.0

I cant get it to work, here is my code:

$("#node-form-save").click(function(){
        event.preventDefault();
        var firstName = $("#node-form-first-name").val();
        var lastName = $("#node-form-last-name").val();
        var dob = $("#node-form-dob").val();
        $.ajax({
            url:'/persons/create',
            type: 'GET',
            datatype: 'json',
            data: {firstName: firstName, lastname: lastName, dob:dob},
            success: function(data){
                alert("succes");
                }
        });
        $('#modal-pop').jqmHide();

    })

I started with a simple test, to see if the sucess function is reached, but I get no alert. What is wrong with my code? The url is correct. The url /persons works, and when I type in /persons/create I get a CakePHP screen saying that I have no view (but I don't want one, I am doing ajax. I list my controller (PersonsController.php) below

class PersonsController extends AppController
{
public $helpers = array('html', 'form');

public function index()
{

}

public function create()
{

}
}

I left the functions blank for now, just so I can get the basic functionality working.

3 Answers 3

1

First: why "get"?? Seems uber insecure for a create function. Do the ajax call by POST instead.

You said you don't want a view, but your code nowhere specifies that. For ajax, and to avoid rendering the view and the layout, do this

public function create() {
    $this->autoRender = false;
}

Also, if you do that, you won't get anything in return, so in the action, you need to echo the values so ajax can recieve it.

And a tip, you can separate the functions you want to excecute with ajax and the normal way with a simple check

public function create() {
    if( $this->request->is('ajax') ) {
        $this->autoRender = false;
        echo 'hello result';
        return;
    }

    //else, handle this action as a normal one and render a view and whatever
}
8
  • You wrote "so in the action, you need to echo the values so ajax can recieve it." What does this mean? Jun 13, 2013 at 20:02
  • It means that if you don't render a view, then what result is the ajax going to get? Nothing. So you echo the result, something like echo "[{id:1}]" or a valid json. That way the success function of jquery can receive a result and parse it.
    – Nunser
    Jun 13, 2013 at 20:04
  • I just added $this -> autorender = false; echo "hello"; to my create() function and the alert still isn't happening. I even changed the alert to be alert(data); and still nothing. Any more ideas? Jun 13, 2013 at 20:09
  • If it helps anything: I originally made the ajax and forgot to first make the create() function in the controller. At that time, the parameters showed up on the url (because is a GET... Yes, its only for testing.) But after I made the create(), now I don't see any parameters in the url when I click save. Jun 13, 2013 at 20:12
  • Debug. Check with the fail option (jqXHR.fail(function(jqXHR, textStatus, errorThrown) {});) of jquery ajax and see what is returning. If it's not returning anything there either, then I think you're probably hitting the wrong url. Also, if you are still testing with GET, then the params need to be inside create (create($fName, $lName, $dob)).
    – Nunser
    Jun 13, 2013 at 20:15
0

You need to pass the "event" to the function:

$("#node-form-save").click(function(event){
        event.preventDefault();
        ...
}
1
  • Didn't help. But I don't think you need to do that, I prevented the default action even without passing event in... Jun 13, 2013 at 20:19
0

this is how i have done this.hope it helps

$(document).ready(function() {
    $(".submit").click(function(event){//my submit button has class name 'submit'
        event.preventDefault();

        $.ajax({
            type:"POST",
            url:"/cakephp/messages/create/",
            success : function(data) {
                alert('success');

            }
        });
    });
});

Controller

public function create(){
    if( $this->request->is('ajax') ) {
        $this->autoRender = false;
        echo 'hello result';
        return;
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.