jQuery提交到iframe中 [英] jQuery submit into an iframe
问题描述
我通过jquery将表单提交到iframe中.我从JSON代码的php文件中得到了结果.如何在JavaScript中使用此字符串?
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 target="iframe" method="post" action="upload.php" enctype="multipart/form-data">
...
</form>
将来自upload.php的响应打印到iframe中
{"datafile":{"name":"","type":"","tmp_name":"","error":4,"size":0}}
如果来自php的响应到达,然后访问数据文件的error属性,注册回调的最佳方法是什么?
whats the best way to register a callback if the response from the php arrived, and then access to the datafiles 's error property?
推荐答案
尝试将加载处理程序附加到iframe.在处理程序内的$('#iframe').on('load', handler)
中,您可以获取iframe主体并提取json并进行解析以供使用.您可以在处理程序中执行类似的操作
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.
这篇关于jQuery提交到iframe中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!