如何从 iFrame 访问 servlet 结果?

发布于 2024-10-05 13:02:44 字数 1629 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2024-10-12 13:02:44

我找到了解决办法。我想我在这里太天真了。
我在 iframe 中声明一个变量,并将其在 servlet 中分配给 iFrame html 中的 gson 变量。然后我读取 uploadComplete 函数中的变量。这是

<form name='myform' id='myform' method="POST" enctype="multipart/form-data" action="http://localhost:9090/bankUI/loadaccountstatement" 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 newStatement;
    </script>
</iframe>

servlet 代码:

response.setContentType("text/html");
                        response.getWriter().println("<html><body onload=\"window.parent.uploadComplete();\">"+
                                "<script type=\"text/javascript\">" +
                                "parent.document.newStatement = "+gsonTable+";" +
                                "</script>"+
                                "<div> </div>"+
                                "</body></html>");
                        response.getWriter().close();

函数

function uploadComplete() {
    //the variable newStatement in the iframe containing the returned variable
    var stat=parent.document.newStatement;
    //assigning a variable in the general context
    var myVar=this.currentSession=stat;

}

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

<form name='myform' id='myform' method="POST" enctype="multipart/form-data" action="http://localhost:9090/bankUI/loadaccountstatement" 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 newStatement;
    </script>
</iframe>

the servlet code:

response.setContentType("text/html");
                        response.getWriter().println("<html><body onload=\"window.parent.uploadComplete();\">"+
                                "<script type=\"text/javascript\">" +
                                "parent.document.newStatement = "+gsonTable+";" +
                                "</script>"+
                                "<div> </div>"+
                                "</body></html>");
                        response.getWriter().close();

the function

function uploadComplete() {
    //the variable newStatement in the iframe containing the returned variable
    var stat=parent.document.newStatement;
    //assigning a variable in the general context
    var myVar=this.currentSession=stat;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文