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'></script>
<script type='text/javascript'
src='http://github.com/malsup/form/raw/master/jquery.form.js?v2.43'></script>
<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?
As far as I can tell from looking at the
.ajaxSubmitcode, 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>.