2010-11-28 1 views
0

Ich benutze Javascript. Ich verwende einen iframe, um eine Datei auf ein Servlet hochzuladen. Ich benutze ein Java-Servlet, das den Post korrekt empfängt und ein Gson-Objekt zurückgibt. Ich kann jedoch nicht auf das zurückgegebene Objekt vom Iframe zugreifen. HierWie greife ich auf die Servlet-Ergebnisse von einem iFrame zu?

ist die 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> 

das Servlet tut, was es braucht und gibt

> 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(); 

wo die gsonTable ist { "Spitznamen": "defaultStatname", "Datum": "1/1/2010/"}

Wie bekomme ich das Gson-Objekt aus dem div?

In meiner Funktion

function uploadComplete() { 
    var frame=parent.document.getElementById('myFrame'); 
    var pippo=frame.contentDocument; 
    var div = pippo.getElementById('resu'); 
    var myvar=div.innerHTML; 
    myvar=eval(myvar); } 

wenn ich eval (myvar) führe ich bekommen „ungültig Label“ Ich bin ziemlich überrascht, ein Gson Objekt da ist es sollte die Zeichenfolge eval in Ordnung sein. Ich bin mir sicher, dass ich irgendwo einen Fehler mache, aber ich kann es nicht finden. Vielleicht sollte ich das Gson-Objekt nicht im Div speichern und es gibt eine bessere Lösung. Jede Hilfe wäre toll /f

Antwort

0

Ich habe eine Lösung gefunden. Ich denke, ich war hier ziemlich naiv. Ich deklariere eine Variable im iframe und weise sie im Servlet der gson-Variable im iFrame html zu. Dann lese ich die Variable innerhalb der uploadComplete-Funktion. Hier ist sie

<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> 

der 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(); 

die Funktion

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; 

} 
Verwandte Themen