Getting HTTP status code from loaded iframe using Javascript

I used the jQuery Form plugin to submit an asynchronous form. For forms containing files, it copies the form to a hidden iframe, submits it, and copies the contents of the iframe. The problem is that I cannot figure out how to find which HTTP status code was returned by the server. For example, if the server returns 404, the data from the iframe will be copied as normal and will be processed as a regular response.

I tried scrolling iframes looking for some status_code attribute but couldn't find anything like it.




The $.ajax() function cannot be used because it does not support file downloads. The only way I can download files asynchronously that I know of is by using a hidden iframe method.

+43
jquery ajax forms iframe
Aug 29 '08 at 20:31
source share
2 answers

You cannot get JS page titles, but you can distinguish error from success: Try something like this:

 <script type="text/javascript"> var uploadStarted = false; function OnUploadStart(){ uploadStarted = true; } function OnUploadComplete(state,message){ if(state == 1) alert("Success: "+message); else if(state == 0 && uploadStarted) alert("Error:"+( message ? message : "unknow" )); } </script> <iframe id="uploader" name="uploader" onload="OnUploadComplete(0)" style="width:0px;height:0px;border:none;"></iframe> <form id="sender" action="/upload.php" method="post" target="uploader" enctype="multipart/form-data" onsubmit="OnUploadStart()"> <input type="file" name="files[upload]"/> <input type="submit" value="Upload"/> </form> 

On the server side:

 /* file: upload.php */ <?php // do some stuff with file print '<script type="text/javascript">'; if(success) print 'window.parent.OnUploadComplete(1,"File uploaded!");'; else print 'window.parent.OnUploadComplete(0, "File too large!");'; print '</script>'; ?> 
+17
Dec 04 '09 at 16:09
source

You cannot get the HTTP status code from the downloaded "iframe" directly. But when an HTTP error occurs, the server will return nothing iframe. So iframe is not happy. you can check the iframe body when the iframe body is empty, use ajax with the same url to get the response from the server. Then you can get the http status code from the response.

-one
Mar 16 '15 at 2:13
source



All Articles