2012-01-06 141 views
-3

我写了一个JavaScript,它使用Jquery.post从Web应用程序加载网页。我在iframe中显示页面。该网页显示在IE中,但不是在Firefox中。我试过使用Firebug,但没有错误,它有一个302 OK笔记。我试图更改也没有工作的Jquery源代码。试过JSON,也没有工作。它超过3天我正在尝试解决这个问题。我尝试了很多方法,但效果并不好。Jquery.post不工作​​在Firefox中

<html> 
    <head> 
    <script  type="text/javascript"src="http://ajax.microsoft.com/ajax/jquery/jquery1.4.2.min.js"> 
    </script> 
    <script type="text/javascript"> 


    function callajax() 
    { 
     var iframe =document.createElement("iframe"); 
     iframe.style.width="100%"; 
     iframe.style.height="100%"; 
     //app.getContentEl().appendChild(iframe); 
     document.body.appendChild(iframe); 
     jQuery.post('http://localhost:9090/simpleapp/formproc1',  {'param':'rajat'},function(html){ 


     var doc =iframe.contentWindow.document; 
     doc.write(html); 
     doc.close(); 
     }); 
     } 



     </script> 
     </head> 

     <body> 
     <p>Start typing a name in the input field below:</p> 
     <span></span> 
     <div id="display"></div> 
     First name: 

     <input type="text" /> 

     <button onclick="callajax()">Click me</button> 
     </body> 
     </html> 

我也会附上post方法,因为它也可能是一个错误的方法。

 doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

// TODO自动生成方法存根

 PrintWriter out = response.getWriter(); 

     String par = request.getParameter("param"); 

     System.out.print("Hiii this is inside POST method"); 

     //out.println("<data><param>"+par+"</param></data>"); 

     //out.println(par); 

     //out.flush(); 

     //System.out.print(par); 

     response.sendRedirect("first.jsp"); 

     // out.println("{\"redirect\":\"first.jsp\"}"); 

     } 
+0

尝试把警报和检查方法按钮点击后调用。 – 2012-01-06 06:11:23

+0

描述它如何“不起作用”。它是否创建iframe? iframe是否完全填充?你有没有尝试在firebug中手动调用jQuery.post函数? – codersarepeople 2012-01-06 06:12:38

+0

非常感谢您的迅速响应。 Firebug没有显示任何错误。但是响应为空。请求成功通过。并且创建了iframe,但网页未填充到iframe中。现在你能帮我吗 – user1132583 2012-01-06 06:37:52

回答

1

这是因为Same origin policy的。你不能使用ajax来调用外部网站。如果你真的想使用,你必须使用JSONP。或者你可以使用serverside代理。意味着,在服务器端调用外部站点,并对该web服务执行ajax调用。

看到我的回答, $.ajax call working fine in IE8 and Doesn't work in firefox and chrome browsers

+0

谢谢你的回答。但是如果使用JSONP,如果POSt方法响应是网页会发生什么。网页是否会显示在iframe中?我的意思是如果网页可以作为参数传递回调函数。请在此点亮一下。 – user1132583 2012-01-06 09:08:30