title: JavaScript ajax request to servlet call error callback function
body: <p>I have the following code snippet:</p>
  
  <pre><code>$(d).ready(function() {
      $.ajax({
          url : "http://localhost:8080/ProjTest/TestServlet",
          type : "POST",
          dataType : "text",
          data : { test : "test" },
          error : function() {
              alert(1);
          },
          success : function(data) {
              alert(2);
          }
      });
  });
  </code></pre>
  
  <p>and in my servlet:</p>
  
  <pre><code>protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      Printer out = res.getWriter();
      String paramTest = req.getParameter("test");
      System.out.println("1");
      out.print(paramTest);
      out.close();
      System.out.println("2");
  }
  </code></pre>
  
  <p>When I run, in console:</p>
  
  <pre><code>1
  2
  </code></pre>
  
  <p>But, in javascript call error function !!!</p>
  
  <p>Any idea ???</p>
  
  <hr>
  
  <p>Solved:</p>
  
  <p>I changed the servlet to</p>
  
  <pre><code>protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
      res.setHeader("Access-Control-Allow-Origin", "*");
      Printer out = res.getWriter();
      String paramTest = req.getParameter("test");
      System.out.println("1");
      out.print(paramTest);
      out.close();
      System.out.println("2");
  }
  </code></pre>
  
  <p>I added the line:</p>
  
  <p><em>res.setHeader("Access-Control-Allow-Origin", "*");</em></p>