title: I can't retrieve responseText from servlet using AJAX
body: <p>I have a servlet file called <code>NewServlet.java</code>. This servlet is called by my AJAX script to retrieve the response.</p>
  
  <p>I have verified the servlet by testing it in a browser.</p>
  
  <p>But when I call it from my AJAX script it gives me a blank <code>responseText</code> and an error that says </p>
  
  <blockquote>
    <p>XMLHttpRequest cannot load
    <a href="http://localhost:8084/WebApplication1/NewServlet" rel="nofollow">http://localhost:8084/WebApplication1/NewServlet</a>.
    Origin null is not allowed by
    Access-Control-Allow-Origin</p>
  </blockquote>
  
  <p><strong>NewServlet.java</strong></p>
  
  <pre><code>import java.io.*;
  import java.net.*;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class NewServlet extends HttpServlet {
  
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
  
  
          out.println("&lt;option value='1'&gt;one&lt;/option&gt;");
          out.println("&lt;option value='2'&gt;two&lt;/option&gt;");
          out.println("&lt;option value='3'&gt;three&lt;/option&gt;");
          out.println("&lt;option value='4'&gt;four&lt;/option&gt;");
          out.println("&lt;option value='5'&gt;five&lt;/option&gt;");
          out.close();
      }
  
  
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
      }
  
  
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
      }
  
  
      public String getServletInfo() {
          return "Short description";
      }
  
  }
  </code></pre>
  
  <p><strong>test.html</strong></p>
  
  <pre><code>&lt;html&gt;
  
  &lt;head&gt;
      &lt;script language = "javascript"&gt;
          var xmlDoc = 0;
          var xhttp = 0;
          function reciveData()
          {
  
              if (window.XMLHttpRequest)
              {
                  xhttp=new XMLHttpRequest();
              }
              else // IE 5/6
              {
                  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
              xhttp.onreadystatechange = redirectUser;        
              xhttp.open("GET","http://localhost:8084/WebApplication1/NewServlet",true);
              xhttp.send();
              }
  
          function redirectUser()
          {
              if (xhttp.readyState == 4)
              {
                  log = 0;
                  xmlDoc = xhttp.responseText;
                  alert(xmlDoc);
              }
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body onload="reciveData()"&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Can some one point me in a right direction ?</p>
  
  <p>Thanks.</p>