views:

200

answers:

3

Here's my test case. If the form is posted, a 500 error response is sent. If not, the form is sent.

If the file input tag is commented out, the error handler is called. If the file input tag is present, the error handler isn't called. I think this might have something to do with the fact that jQuery needs to use an iframe to handle the upload and iframes don't seem to respond to the error handler.

Edit: If I add iframe: true to the options passed to ajaxSubmit to force the use of an iframe, the non-file-upload case stops working also, so it definitely has to do with the iframe.

Edit2: I'm using the jQuery Form Plugin.

<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST') {
        header('HTTP/1.1 500 Internal Server Error');
        die;
    } else {?>
    <html><head>
        <script type='text/javascript'
          src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js?ver=2.9.2'&gt;&lt;/script&gt;
        <script type='text/javascript'
          src='http://github.com/malsup/form/raw/master/jquery.form.js?v2.43'&gt;&lt;/script&gt;
        <script type="text/javascript">
            jQuery(document).ready(function() {
                jQuery('a').click(function() {jQuery('form').ajaxSubmit({error: function(){alert('error handler called');}})});
            });
        </script>
    </head><body>
        <form method="POST">
            <input type="text" name="mytext" />
            <input type="file" name="myfile" /><!-- comment this element out -->
            <input type="hidden" name="blah" value="blah" />
            <a>submit</a>
        </form>
    </body></html>

<?php }

Is there any way to get the error handler to be called in both situations?

A: 

It seems to me that you use incorrect Form Submission plugin ajaxSubmit(). If the code of ajaxSubmit correspond to the code from http://be.twixt.us/jquery/formSubmission.php, parameters like {error:..,} will be ignored by ajaxSubmit().

ajaxSubmit() use $.post to make ajax request, so you can define you error callback by jQuery.ajaxSetup() (see http://api.jquery.com/jQuery.ajaxSetup/ and http://api.jquery.com/jQuery.ajax/)

UPDATED: How I can see from the source of plugin which you used (see http://github.com/malsup/form/raw/master/jquery.form.js?v2.43) $.ajax(options); will be use only in else part of the following if statement:

if (files.length && options.iframe !== false) || options.iframe || found || multipart) {
    if (options.closeKeepAlive)
        $.get(options.closeKeepAlive, fileUpload);
    else
        fileUpload();
} else
     $.ajax(options);

so in all other cases you can not use error as a parameter of ajaxSubmit(). So you can set {closeKeepAlive: true} as a parameter of ajaxSubmit() and don't forget to set beforeerrorcallback byjQuery.ajaxSetup()` as I described before.

Oleg
The documentation for the form submission plugin (http://jquery.malsup.com/form/#options-object) explicitly says that the standard `$.ajax` options are allowed. Also, it works when the form doesn't contain a file upload.
scompt.com
A: 

As far as I can tell from looking at the .ajaxSubmit code, no attempt is made to detect or handle errors in the case that you've submitted to an <iframe>. I suspect that's because there's just no way for it to learn about the error code that came back from the server in the HTTP response targeted at the <iframe>. It does call the "success" and "complete" plugins, and it tries to cobble together a fake xhr object using whatever it finds in the DOM in the <iframe>.

Pointy
Thanks for the push in the right direction. I forked the plugin and added some error handling in the code path that's being taken. http://github.com/scompt/form
scompt.com
A: 

LTTP, but to get this to work without using a forked jquery.form plugin, set the dataType option to 'json':

form.ajaxSubmit({ datatype: 'json', ...

then make the server return a trivial response on success (which gets interpreted as valid JSON), instead of the bare 200 OK response. Can be as simple as:

header('HTTP/1.1 200 OK');
echo(0);

This convinces the plugin to process a 5XX error response.

Ben Scott