【问题标题】:Reading response from (simulated) asynchronous file upload with jQuery, IFRAME使用 jQuery,IFRAME 从(模拟)异步文件上传中读取响应
【发布时间】:2012-07-10 20:31:22
【问题描述】:

我正在通过定位 iframe 来模拟异步文件上传,如下所示:

<form id="upload_form" action="upload.php" method="POST" enctype="multipart/form-data" target="upload_iframe">
  <input type="file" name="upload_input" />
  <iframe id="upload_iframe" name="upload_iframe" src=""></iframe>
</form>

选择文件后,表单提交如下(使用jQuery):

$('#upload_input').change(function() {
  $('#upload_form').submit();
});

文件被上传,JSON 响应被发送到 iframe。在 Firebug 中,我可以看到 iframe 的内容是:

<html>
<head></head>
<body>{"success":true}</body>
</html>

我正在使用 iframe 的加载事件来等待响应。加载事件正在触发。 (它实际上会触发两次,这是一个小问题。)但是,我不知道如何阅读响应。我尝试了各种调用,但没有一个返回任何内容:

$('#upload_iframe').find('body')
$('#upload_iframe').html()
$('#upload_iframe').text()

我有什么遗漏吗?

【问题讨论】:

    标签: jquery iframe file-upload


    【解决方案1】:

    我找到了一个可行的解决方案,尽管它似乎不太理想。此代码进入 iframe 加载事件处理程序:

    if (!$(this)[0].contentDocument.childNodes[0].children[1].childNodes[0]) 
    {
      return;
    }
    var response = $(this)[0].contentDocument.childNodes[0].children[1].childNodes[0].wholeText;
    var json = $.parseJSON(response);
    

    【讨论】:

      【解决方案2】:

      这应该可以完成你的工作 $('#upload_iframe').find('body').text()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-04-20
        • 2019-12-20
        • 2018-06-11
        相关资源
        最近更新 更多