jQuery submit into an iframe

2019-07-31 02:21发布

i submit my form into an iframe via jquery. i got the result from the php file in JSON code. How can i use this string in my javascript?

form

<form target="iframe" method="post" action="upload.php" enctype="multipart/form-data">
...
</form>

response fromupload.php printed into the iframe

{"datafile":{"name":"","type":"","tmp_name":"","error":4,"size":0}}

whats the best way to register a callback if the response from the php arrived, and then access to the datafiles 's error property?

1条回答
欢心
2楼-- · 2019-07-31 02:39

Try to attach a load handler to the iframe. $('#iframe').on('load', handler) inside the handler you can get the iframe body and extract the json and parse it for use. You can do something like this in the handler

var $body = $(this.contentDocument || this.contentWindow);
if (!$body.is('body')){
    $body = $body.find('body');
}
var data = $.parseJSON($body.html());

Note Same Origin Policy applies so this will not work if the post is made to another origin.

查看更多
登录 后发表回答