Q: Web services in Django I have a simple Java application, which reacts to web service request.
When the user presses a button, a message is sent to the Java application.
If the message is "ping", then the application responds with "pong", otherwise - "uknown message".
Code of the Java application:
@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;
        }
    }

}

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):
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>JavaScript Ping-Pong Client</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript">

        //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 && http.status == 200) {
                    alert(http.responseText);
                }
            };
            http.send(params);
        }
    </script>
</head>
<body>
    <button onclick="test('ping')">Test</button>
    <button onclick="testWithJQuery('ping')">Test using jQuery</button>
</body>
</html>

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.
Django generates following HTML code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title></title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
    //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 && http.status == 200) {
                alert(http.responseText);
            }
        };
        http.send(params);
    }
</script>


</head>
<body>
<button onclick="test('ping')">Test</button>
</body>
</html>

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.
How can I fix this, i. e. make sure that the response of the Java application is displayed in Django?
UPD: Here's the detailed error description in the "Network" pane of Chrome:


A: This is not an straight answer but rather some tips to help you diagnose the problem; I'm posting here because it is too big for a comment.


*

*open the Chrome debugger (shif+ctrl+i) or Firefox Firebug and make the Network tab active.

*inspect the post made by both versions and compare headers and body


If there is distinct behavior, there should be some differences between both posts.
[update]

I found the error - see the screenshot in the question. The error is "XMLHttpRequest cannot load localhost:8345/rest/ping-pong. Origin localhost:8000 is not allowed by Access-Control-Allow-Origin.".

The error is due to security constraints in the browser (same origin policy). There are some workarrounds (source: wikipedia):

To enable developers to, in a controlled manner, circumvent the same origin policy, a number of "hacks" such as using the fragment identifier or the window.name property have been used to pass data between documents residing in different domains. With the HTML5 standard, a method was formalized for this: the postMessage interface, which is only available on recent browsers. JSONP and cross-origin resource sharing can also be used to enable ajax-like calls to other domains.2 easyXDM can also be used to easily work around the limitation set in place by the same origin policy. It is a lightweight, easy-to-use and self-contained Javascript library that makes it easy for developers to communicate and expose javascript APIs across domain boundaries.

I've used the subdomain hack with success, see this answer:


*

*Ways to circumvent the same-origin policy
