Question

I am doing a cross-site ajax to java data transaction(Not sure if I named that correctly, so please forgive me about that). Part of code in Java file:

BufferedReader input =
            new BufferedReader(new InputStreamReader(connectionsocket.
            getInputStream()));
DataOutputStream output =
            new DataOutputStream(connectionsocket.getOutputStream());
...
output.writeChars("some random text");
output.close();

Also I have index.php file with some jQuery:

$(document).ready(function() 
    {
        $("#send_data").click(function(){
            $.ajax({
                type: 'get',
                dataType: 'text',
                url: 'http://localhost:1024/'+$("#command").val(),
                success: function(data) {console.log(data);},
            error: function() { console.log("Error"); }
        })
    });
});

The command is sent correctly and received in Java side correctly. Then the request from java to ajax is 200 OK too. The output is also working. (For example if I remove output.close(), I do see in the firebug, that it is waiting for the output to be closed.)

The only problem is, no matter what I do I get no response text. It's always an empty string :(

Was it helpful?

Solution

cross-site ajax to java data transaction

and

index.php

and

Java

Implies you are violating same origin policy. You must use same host:port combination to retrieve the webpage (or at least the javascript version of the code that does AJAX) and send AJAX requests.

In other words, if your JS comes from localhost:80 and you are trying to send AJAX request to localhost:1024, you are violating security policies.

There are ways to do cross-domain AJAX like jsonp, but do you really need that? I would suggest serving jQuery code from the servlet, or eliminate PHP at all, or, even better, rewrite everything in Scala or Erlang. :)

OTHER TIPS

in your code you create dataType:"xml" try to use with text.because in your response i don't see the xml format,you're create response with format text.

            $.ajax({
                type: 'get',
                dataType: 'text',
                url: 'http://localhost:1024/'+$("#command").val(),
                success: function(data) {console.log(data);},
            error: function() { console.log("Error"); }

read this for option ajax request http://api.jquery.com/jQuery.ajax/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top