0

I send request to servlet with a link and want to receive response with html code of this link. Servlet receives the link, handles it and sends html code in response, but I can't receive it with ajax.

Request & Response in servlet - it works fine!

request.getParameter("url");

response.getWriter().print(responseText);

jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title></title>
</head>
<body >
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<div height="100%">

  <div height="30%">
 <div>Some text here</div>
    <div> <form id="submitForm">
      <input type="text" width="600px" id="url">
      <button id="ajaxSubmit" type="submit" >Submit</button>
    </form></div>
    <br/>
    <div id="div2"></div>
  </div>
  <div height="60%">
  <iframe id="FileFrame" name="iframe" src="" name="myFrame" height="100%" width="100%" frameborder="0" >
  </iframe>
  </div>
  <script type="text/javascript">
$("#submitForm").submit(function(){
      console.log("onclick");
      var url = $("#url").val();
      $.post('proxyServer', {url: url}, function(responseText) {
        console.log("text received");
        var iframe = document.getElementById('FileFrame');
        var iframedoc = iframe.document;
        if (iframe.contentDocument)
          iframedoc = iframe.contentDocument;
        else if (iframe.contentWindow)
          iframedoc = iframe.contentWindow.document;
        if (iframedoc) {
          //iframedoc.open();
          iframedoc.write(responseText);
          iframedoc.close();
        } else {
          alert('Cannot inject dynamic contents into iframe.');
        }
        }).error(function(p1, p2, p3){
        alert("error!");
        console.log(p1 + p2 + p3);
      });
    });
  </script>
</div>
</body>
</html>
Geha
  • 1,345
  • 2
  • 10
  • 16
  • Have you checked if responseText is actually carrying the html-data before being sent out by servlet? Are you getting any of these console error? – Andrew Ribeiro Jun 29 '16 at 23:14
  • Yes, I've checked, responseText is really carrying the html before being sent. I've already received this html code but not with ajax. So I'm sure that the problem is in ajax, not in servlet – Geha Jun 30 '16 at 05:46

0 Answers0