1

I have the following JQuery code:

 $('#dropdown').bind('change', function() {       
    var formData = $('#newform').serialize()
    $.ajax({
        type: 'POST',
        url: baseUrl+"crm/orders/ajaxEdit",
        data: formData,
        success: function(){ 
            console.log("ok");
        },
        error: function() {
            console.log("not ok");
        }
    });
 });

The ajaxEdit action is very simple:

    function ajaxEdit() {
        $this->layout = 'ajax';
        $this->autoRender = false;
        $this->Order->save($this->data['Order']);
    }

It works fine in that the data from the form is posted to the correct action in my CakePHP controller.

However, regardless of whether the insert query in CakePHP is successful or not, the "ok" message gets echoed to the console unless the HTTP request fails or something.

How can I get cake to send an error back to JQuery that JQuery will recognize as an error and echo "not ok" to the console in the event that there's a validation error or some other insert error in the Cake action?

Joseph
  • 2,737
  • 1
  • 30
  • 56

1 Answers1

0

try this: jquery code

$('#dropdown').bind('change', function() {       
    var formData = $('#newform').serialize()
    $.ajax({
        type: 'POST',
        url: baseUrl+"crm/orders/ajaxEdit",
        data: formData,
        success: function(output){ 
            var x =  jQuery.parseJSON( output );
            if(x.success == 1){
                console.log("ok");
            }else{
                console.log("not ok");
            }
        },

    });
});

ajax edit action

function ajaxEdit() {
    $this->layout = 'ajax';
    $this->autoRender = false;
    if($this->Order->save($this->data['Order'])){
        $res = array('success'=>1);
        $str=json_encode($res);
        echo $str; 
    } 
    else{
        $res = array('success'=>0);
        $str=json_encode($res);
        echo $str; 
    }
}
Krishna
  • 1,540
  • 2
  • 11
  • 25
  • that looks like it would work, but is there any particular reason why I can't use what appears to be the built-in error handling capabilities of JQuery and put my error logic in `error:` ? Isn't there anything I can return from CakePHP to make JQuery see the error as an error rather than as a success with a key to trigger a conditional? If not, never mind, I'll mark your answer as correct.... but I just wanted to be clear first. – Joseph Jul 16 '12 at 07:51
  • for that in ajax edit action else part you can use $invalid = $this->Order->invalidFields(); echo json_encode($invalid); and by this you can get errors in view so jquery can handle it. – Krishna Jul 16 '12 at 12:24
  • yes, but my point is that everything is still being processed under `success:` in JQuery so JQuery doesn't 'know' it's an error. What I want to do is pass a response from my Cake app to JQuery that JQuery will recognize as an error and execute the code under `error:` – Joseph Jul 17 '12 at 02:54
  • I got your problem and tried to find out the solution but unfortunately I was unable to relate the validation errors of cakephp to jQuery..... – Krishna Jul 19 '12 at 07:42
  • Perhaps the point of the JQuery error handler is just to give an error if the ajax connection fails in some way. In the absence of any answers from anyone else, I'm going to mark your question as correct as it is a functional solution. – Joseph Jul 20 '12 at 02:52