title: Web services in Django
body: <p>I have a simple Java application, which reacts to web service request.</p>
  
  <p>When the user presses a button, a message is sent to the Java application.</p>
  
  <p>If the message is "ping", then the application responds with "pong", otherwise - "uknown message".</p>
  
  <p>Code of the Java application:</p>
  
  <pre><code>@Path("/ping-pong")
  public class PingPongService {
  
      @POST
      @Produces("text/plain")
      public String test(@FormParam("message") final String aMessage) {
          System.out.println("message from client: " + aMessage);
          if ("ping".equalsIgnoreCase(aMessage)) {
              System.out.println("ping.equalsIgnoreCase(aMessage)");
              return "pong";
          } else {
              System.out.println("Unknown message");
              return "unknown message " + aMessage;
          }
      }
  
  }
  </code></pre>
  
  <p>Inside the WAR file, I have following HTML code, which works (when I press one of the buttons, I get the response from the Java application and it is displayed in the message box):</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;JavaScript Ping-Pong Client&lt;/title&gt;
      &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
  
          //This function send post request to the server using jQuery
          function testWithJQuery(message){
              $.post('http://localhost:8345/rest/ping-pong', { message: message }, function(data) {
                  alert(data);
              });
          }
  
          //This function send post request to the server using a low-level XmlHttpRequest instead of the jQuery
          //(no dependencies on external libraries)
          function test(message){
              var http = new XMLHttpRequest();
              var url = "http://localhost:8345/rest/ping-pong";
              var params = "message=" + message;
              http.open("POST", url, true);
              http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
              http.setRequestHeader("Content-length", params.length);
              http.setRequestHeader("Connection", "close");
              http.onreadystatechange = function() {
                  if(http.readyState == 4 &amp;&amp; http.status == 200) {
                      alert(http.responseText);
                  }
              };
              http.send(params);
          }
      &lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;button onclick="test('ping')"&gt;Test&lt;/button&gt;
      &lt;button onclick="testWithJQuery('ping')"&gt;Test using jQuery&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I want to do exactly the same thing (user presses a button, a message is sent to the Java application and its response displayed in an alert message window) in a Django application.</p>
  
  <p>Django generates following HTML code:</p>
  
  <pre><code>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
  
  &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      //This function send post request to the server using jQuery
      function testWithJQuery(message){
          $.post('http://localhost:8345/rest/ping-pong', { message: message }, function(data) {
              alert(data);
          });
      }
  
      //This function send post request to the server using a low-level XmlHttpRequest instead of the jQuery
      //(no dependencies on external libraries)
      function test(message){
          var http = new XMLHttpRequest();
          var url = "http://localhost:8345/rest/ping-pong";
          var params = "message=" + message;
          http.open("POST", url, true);
          http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
          http.setRequestHeader("Content-length", params.length);
          http.setRequestHeader("Connection", "close");
          http.onreadystatechange = function() {
              if(http.readyState == 4 &amp;&amp; http.status == 200) {
                  alert(http.responseText);
              }
          };
          http.send(params);
      }
  &lt;/script&gt;
  
  
  &lt;/head&gt;
  &lt;body&gt;
  &lt;button onclick="test('ping')"&gt;Test&lt;/button&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When I press the "Test" button in Django-generated code, the Java application receives the message (I see this in the console output), but the response of the Java application is not displayed in Django web site, even though the HTML code is identical to the one in the WAR file.</p>
  
  <p>How can I fix this, i. e. make sure that the response of the Java application is displayed in Django?</p>
  
  <p>UPD: Here's the detailed error description in the "Network" pane of Chrome:</p>
  
  <p><img src="https://i.stack.imgur.com/JCmAV.png" alt="enter image description here"></p>