如何从 iFrame 访问 servlet 结果?
我正在使用 JavaScript。我使用 iframe 将文件上传到 servlet 。我使用 java servlet 正确接收帖子并返回 gson 对象。但是我无法从 iframe 访问返回的对象。
形式
<form name='myform' id='myform' method="POST" enctype="multipart/form-data" action="http://localhost:9090/myServlet" target="myFrame" >
<td> <input type="file" size=20 name="fname"> </td>
<td> <input type="Submit" value="Upload"> </td> </form>
</tr></table>
<iframe src="" id="myFrame" name="myFrame" style="width: 110px; height: 110px;">
<script type="text/javascript">
var accountList=null;
</script>
</iframe>
这是servlet 执行所需操作并返回的
> response.setContentType("text/html");
> response.getWriter().println("<html><body
> onload=\"window.parent.uploadComplete();\">"+
> "<div id='resu' name='resu'>" +
> gsonTable+
> "</div>"+
> "</body></html>"); response.getWriter().close();
,其中 gsonTable 为 {"nickname":"defaultStatname","date":"1/1/2010/"}
如何从 div 中获取 gson 对象?
在我的函数中,
function uploadComplete() {
var frame=parent.document.getElementById('myFrame');
var pippo=frame.contentDocument;
var div = pippo.getElementById('resu');
var myvar=div.innerHTML;
myvar=eval(myvar); }
当我执行 eval(myvar) 时,我得到“无效标签” 我很惊讶,因为作为一个 gson 对象,评估字符串应该没问题。 我确信我在某个地方犯了错误,但我找不到它。也许我根本不应该将 gson 对象存储在 div 中,并且有更好的解决方案。 任何帮助都会很棒 /f
I am using javascript. I use an iframe to upload a file to a servlet . I use a java servlet that correctly receives the post and returns a gson object. However I cannot access the returned object from the iframe.
Here is the form
<form name='myform' id='myform' method="POST" enctype="multipart/form-data" action="http://localhost:9090/myServlet" target="myFrame" >
<td> <input type="file" size=20 name="fname"> </td>
<td> <input type="Submit" value="Upload"> </td> </form>
</tr></table>
<iframe src="" id="myFrame" name="myFrame" style="width: 110px; height: 110px;">
<script type="text/javascript">
var accountList=null;
</script>
</iframe>
the servlet does whatever it needs and returns
> response.setContentType("text/html");
> response.getWriter().println("<html><body
> onload=\"window.parent.uploadComplete();\">"+
> "<div id='resu' name='resu'>" +
> gsonTable+
> "</div>"+
> "</body></html>"); response.getWriter().close();
where the gsonTable is {"nickname":"defaultStatname","date":"1/1/2010/"}
how do I get the gson object off the div?
In my function
function uploadComplete() {
var frame=parent.document.getElementById('myFrame');
var pippo=frame.contentDocument;
var div = pippo.getElementById('resu');
var myvar=div.innerHTML;
myvar=eval(myvar); }
when I perform eval(myvar) I get "invalid label"
I am quite surprised because being a gson object it should be fine to eval the string.
I am sure I am making a mistake somewhere but I cannot find it. Maybe I should not store the gson object in the div at all and there is a better solution.
Any help would be great
/f
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了解决办法。我想我在这里太天真了。
我在 iframe 中声明一个变量,并将其在 servlet 中分配给 iFrame html 中的 gson 变量。然后我读取 uploadComplete 函数中的变量。这是
servlet 代码:
函数
I have found a solution. I think I have been pretty naive here.
I declare a variable in the iframe and assign it in the servlet to the gson variable within the iFrame html. Then I read the variable within the uploadComplete function. Here it is
the servlet code:
the function