1

I'm having trouble getting ajaxSubmit to catch an error:

$(document).ready(function(){

        $("#supportForm").validate({
              rules: {
                 //validation
              },

              messages: {              
                 //messages
              },

              submitHandler: function(form) {
                    $(form).ajaxSubmit({
                        url:"ajax/supportform",
                        type:"GET",
                        dataType: 'json',
                        error: function() { alert('error'); },
                        success: function() {alert('success'); },


                    });
              }

        });  
    })

what do I have to return in my php script to get it to fire the error event?

I've tried returning an array with error=>0 , exit(json_encode('error'=>'0'); etc.

Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
  • change the url to something that doesn't exist - that will give you error to practice with – Scott Selby Nov 27 '12 at 19:09
  • I'm not a PHP developer, but wouldn't simply throwing an exception in your PHP code (and not catching it) make the ajax call fail? – Francis Gagnon Nov 27 '12 at 19:11
  • @Francis Gagnon, That depends on the final error handling. A simple PHP syntax error or something won't cause a fail in the Ajax call. – Mike de Klerk Nov 27 '12 at 19:14
  • @mike - correct, my problem is that there is no documentation that defines what the function is expecting back to be considered an error! – Sean Kimball Nov 27 '12 at 19:19

3 Answers3

5

See Mike de Klerk's link, it provided the clues as to what had to be done to trap an error:

It seems the success & error callbacks have nothing to do with passing a boolean error/success message, but only if the url was successfully called. I expect I am not getting error results as I am using a CMS that is always returning some kind of content - even if it's a 404 or 403 page.

Anyway, I had to return a json string from my php script:

<?php


$response = json_encode(array("error" => "false","message" => "this is the error message"));

return $response;

then parse it in my success callback:

submitHandler: function(form) {
        $(form).ajaxSubmit({
            url:"ajax/supportform",
            type:"POST",
            dataType: 'json',

            error: function(data ) { alert(data); },

            success: function(response) {

            var obj = jQuery.parseJSON(response);

            if(obj.error == "true"){
                alert('error is true');
            }else{
                alert('error is false')
            }

            },


        });
  }
Sean Kimball
  • 4,506
  • 9
  • 42
  • 73
2

Edit: to show some specific error description coming from PHP you should read this question: jQuery Ajax error handling, show custom exception messages

Let your server generate some error, with PHP you can do that like this

<?php
header("HTTP/1.0 404 Not Found");
?>

Check out the documentation of the header function: http://nl.php.net/manual/en/function.header.php

What you are doing here is providing your webbrowser with data that does not belong in the typical HTML source, by which the webbrowser knows something went wrong.

You could actually add all meta data you want to the header of the webpage request, but when the webbrowser does not know what it means it is simply ignored. See the definition or status codes here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Community
  • 1
  • 1
Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76
  • +1, For an application error, a 500 code would proabably be more propriate though – sroes Nov 27 '12 at 19:11
  • this returns a 404 response in the headers, but no data in the document itself - the ajax request is expecting data back. (json) official docs are no help & the error callback is not in any examples. – Sean Kimball Nov 27 '12 at 19:18
  • @Sean Kimball, I understand you want a specific error message, instead of just trying out your error handler on some connection error or something? See the edit is my posed answer. – Mike de Klerk Nov 27 '12 at 19:24
  • @mike - so if I am reading that right the success error have nothing to do with whether the php script returned anything in particular, but just that something was returned? in which case - ?I have not been able to get it to throw an error on 404 or 500 headers ~ even if I introduce an error in the script or move the page. but basically I should be passing a json string back to the success callback and 'handling' there? in which case on what even is it even possible to trigger the error callback? – Sean Kimball Nov 27 '12 at 19:56
  • @Sean Kimball Did you read the added URL in my posed ansewr? http://stackoverflow.com/questions/377644/jquery-ajax-error-handling-show-custom-exception-messages I am pretty sure its what you are looking for. Although your initial question indicates otherwise. Hence you got different answers than the one you are looking for. Be sure to ask exactly what you need to know/want. – Mike de Klerk Nov 27 '12 at 20:00
  • Pretty sure I did, "what do I have to return in my php script to get it to fire the error event?" & none of the suggestions here will trigger the error callback. but your link did provide clues as to what had to be done to catch an application error... – Sean Kimball Nov 27 '12 at 20:31
0

change url to something that doesn't exist to force an error

          function(form) {
                $(form).ajaxSubmit({
                    url:"ajax/supportform001",
                    type:"GET",
                    dataType: 'json',
                    error: function() { alert('error'); },
                    success: function() {alert('success');} // <-was an extra comma
                });
          }
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • also does not work - the 404 page from the site is returned [with data in the results] but the success callback is always triggered. – Sean Kimball Nov 27 '12 at 19:18