ajaxSubmit error capturing

12,871

Solution 1

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')
            }

            },


        });
  }

Solution 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

Share:
12,871
Sean Kimball
Author by

Sean Kimball

Sean is responsible for creating solutions and ‘making’ stuff work. A pretty website does not do much for you if it doesn’t work or doesn’t work right! This is where Sean steps in, assembles your site, adds any custom work and makes sure it all works together. Sean was trained as a mechanical engineer, but has chosen to design websites instead [the hours were better]. Sean has also taught college level website design and has been designing for over twelve years. Sean’s most recent engagement has been “entrepreneur”; running Nexus Digital Productions, a website design and custom programming company. Sean also recently became a Novell Certified Linux Professional.

Updated on November 23, 2022

Comments

  • Sean Kimball
    Sean Kimball over 1 year

    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.

    • Scott Selby
      Scott Selby over 11 years
      change the url to something that doesn't exist - that will give you error to practice with
    • Francis Gagnon
      Francis Gagnon over 11 years
      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?
    • Mike de Klerk
      Mike de Klerk over 11 years
      @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.
    • Sean Kimball
      Sean Kimball over 11 years
      @mike - correct, my problem is that there is no documentation that defines what the function is expecting back to be considered an error!
  • sroes
    sroes over 11 years
    +1, For an application error, a 500 code would proabably be more propriate though
  • Sean Kimball
    Sean Kimball over 11 years
    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
    Sean Kimball over 11 years
    also does not work - the 404 page from the site is returned [with data in the results] but the success callback is always triggered.
  • Mike de Klerk
    Mike de Klerk over 11 years
    @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.
  • Sean Kimball
    Sean Kimball over 11 years
    @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?
  • Mike de Klerk
    Mike de Klerk over 11 years
    @Sean Kimball Did you read the added URL in my posed ansewr? stackoverflow.com/questions/377644/… 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.
  • Sean Kimball
    Sean Kimball over 11 years
    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...