Retrieving HTTP status code from loaded iframe with Javascript Retrieving HTTP status code from loaded iframe with Javascript ajax ajax

Retrieving HTTP status code from loaded iframe with Javascript


You can't get page headers by JS, 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 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>';?>


You can't retrieving HTTP status code from loaded "iframe" directly.But when an http error occured, the server will returned nothing to the "iframe".So the iframe has not content.you can check the iframe body, when the body of iframe is blank, use ajax with the same url to get the response from server. Then you can retrieve the http status code from response.