开发者

Reading response from (simulated) asynchronous file upload with jQuery, IFRAME

I am simulating an asynchronous file upload by targeting an iframe, like so:

<form id="upload_form" action="uplo开发者_JS百科ad.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>

After a file is selected, the form is submitted like so (using jQuery):

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

The file is uploaded, and the JSON response is sent to the iframe. In Firebug, I can see that the iframe's content is:

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

I am using the iframe's load event to wait for the response. The load event is firing. (It actually fires twice, which is a minor issue.) However, I can't figure out how to read the response. I have tried various calls, but none of them return anything:

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

Is there something I am missing?


I have found a solution that works, although it seems less than ideal. This code goes in the iframe load event handler:

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);


this should do your work $('#upload_iframe').find('body').text()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜