2

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 :(

Andrius Naruševičius
  • 8,348
  • 7
  • 49
  • 78

2 Answers2

2
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. :)

Motiejus Jakštys
  • 2,769
  • 2
  • 25
  • 30
  • Then why the step one of Ajax->Java transfer is successful(Java end receives url sent), but step two fails? I assume if I am violating same origin policy, it should fail on step one. Can you explain that? – Andrius Naruševičius Mar 18 '12 at 13:14
  • At which stage blocking should be depends on the browser. If you tried with old enough IE version (say 7 or 8), it *should* work (but no guarantees). – Motiejus Jakštys Mar 18 '12 at 13:37
  • Why don't you just [test1](http://www.webdevout.net/test?0K) [test2](http://pastehtml.com/view/brs1b9um2.html) it? Enjoy firebugging the given links. :) – Motiejus Jakštys Mar 18 '12 at 17:12
  • Thanks for your help. Both are inaccessible from my pc, but the second one is accessible from http://pastehtml.com/view/brs1b9um2.html. I have a feeling it has to do something with my settings in xampp? – Andrius Naruševičius Mar 19 '12 at 06:37
  • No, it's your design. You are **not complying** to the same origin policy. It's just impossible what you are trying to. – Motiejus Jakštys Mar 19 '12 at 14:33
1

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/

viyancs
  • 2,289
  • 4
  • 39
  • 71
  • Yes yes sorry, really sorry, it was just I was desperately trying anything I could think of. I did try "text" for 99% of time, but stupid me writes xml in the question :( Really sorry. I edited that. – Andrius Naruševičius Mar 18 '12 at 08:42
  • np ^^, how about your problem still not fix? – viyancs Mar 18 '12 at 10:06
  • did you try to set response.setContentType("text/html"); ? – viyancs Mar 18 '12 at 10:13
  • do you mean to write it in Java or Ajax? Cannot find this function in either :( – Andrius Naruševičius Mar 18 '12 at 10:36
  • yes, i think you use servlet, if not try to replace `output.close();` to `output.flush();` this is for documentation http://docs.oracle.com/javase/6/docs/api/java/io/DataOutputStream.html#flush%28%29 – viyancs Mar 18 '12 at 10:55
  • thanks, at least I know exactly what I am creating, so I can expand my "googling" :) Will try that asap – Andrius Naruševičius Mar 18 '12 at 11:07
  • As weird as it is, I do output.writeChars("some random text"); System.out.println(output.size()); output.flush(); System.out.println(output.size()); output.close(); System.out.println(output.size()); and I do expect the size to go to 0 after flushing, but it still remains the same for all 3 printouts :O – Andrius Naruševičius Mar 18 '12 at 11:14
  • i have idea how if you use httpservlet response ..? this is for ample http://www.roseindia.net/servlets/HelloWorld.shtml – viyancs Mar 18 '12 at 11:25
  • Yeah I guess I have no other choice. Will install TomCat now. Thanks – Andrius Naruševičius Mar 18 '12 at 11:31