使用 Javascript 从加载的 iframe 中检索 HTTP 状态代码 [英] Retrieving HTTP status code from loaded iframe with Javascript

查看:38
本文介绍了使用 Javascript 从加载的 iframe 中检索 HTTP 状态代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 jQuery Form 插件进行异步表单提交.对于包含文件的表单,它将表单复制到隐藏的 iframe,提交它,然后复制回 iframe 的内容.问题是我不知道如何找到服务器返回的 HTTP 状态代码.例如,如果服务器返回 404,则 iframe 中的数据将被正常复制并视为常规响应.

I used the jQuery Form plugin for asynchronous form submission. For forms that contain files, it copies the form to a hidden iframe, submits it, and copies back the iframe's contents. The problem is that I can't figure out how to find what 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 treated as a regular response.

我试过在 iframe 对象中寻找某种 status_code 属性,但没有找到类似的东西.

I've tried poking around in the iframe objects looking for some sort of status_code attribute, but haven't been able to find anything like that.

$.ajax() 函数不能使用,因为它不支持上传文件.我所知道的异步上传文件的唯一方法是使用隐藏的 iframe 方法.

The $.ajax() function can't be used, because it does not support uploading files. The only way to asynchronously upload files that I know of is using the hidden iframe method.

推荐答案

JS 获取不到页眉,但是可以区分错误和成功:尝试这样的事情:

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>

在服务器端:

/*
  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>';
?>

这篇关于使用 Javascript 从加载的 iframe 中检索 HTTP 状态代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆