how do I access the servlet results from an iFrame?
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=pa开发者_如何学Gorent.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
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;
}
精彩评论