[英]Reading response from (simulated) asynchronous file upload with jQuery, IFRAME
我通過以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的load事件來等待響應。 加載事件正在觸發。 (它實際上會觸發兩次,這是一個小問題。)但是,我不知道如何讀取響應。 我嘗試了各種呼叫,但是沒有一個返回任何東西:
$('#upload_iframe').find('body')
$('#upload_iframe').html()
$('#upload_iframe').text()
我有什么想念的嗎?
這應該做你的工作$('#upload_iframe')。find('body')。text()
我發現了一個可行的解決方案,盡管它似乎並不理想。 這段代碼在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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.