-3

I wrote a javascript that loads a webpage from a web application using Jquery.post. I display the page in iframe . The webpage gets displayed in IE but not in firefox. I tried using Firebug but there is no error , it has a 302 OK note. I tried changing the Jquery source that also did not work. Tried JSON that also did not work. Its more than 3 days i am trying to fix this issue. I tried lot of methods but it was not fruitful.

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

i will also enclose the post method since it might also be an erroneous one.

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

// TODO Auto-generated method stub

      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\"}");

         }
Kristiono Setyadi
  • 5,635
  • 1
  • 19
  • 29
  • try putting alert and check if the method is called after button click. – dku.rajkumar Jan 06 '12 at 06:11
  • Describe how it "doesn't work". Does it create the iframe? Does the iframe get populated at all? Did you try calling the jQuery.post function by hand in firebug? – codersarepeople Jan 06 '12 at 06:12
  • thanks a lot for your very swift response. Firebug does nt show any error . But the response is empty.The request gets through successfully.And the iframe is created but the web page is not populated in the iframe. Can u help me now – user1132583 Jan 06 '12 at 06:37

1 Answers1

1

this is because of the Same origin policy. you cannot use ajax to call external sites. if you really want to use, you have to use JSONP. Or you can use serverside proxy for this. means, call external site in the server side and do ajax call to the that webservice.

see my answer, $.ajax call working fine in IE8 and Doesn't work in firefox and chrome browsers

Community
  • 1
  • 1
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
  • Thanks for your answer . But if JSONP is used , what will happen if the POSt method response is a webpage. Will the web page be displayed in iframe? i mean if web pages can be passed as arguments the call back function.Please throw some light on this . – user1132583 Jan 06 '12 at 09:08