0

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

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

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?

2 Answers 2

0

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.