简体   繁体   中英

Read AJAX post content using JAVA socket server

I set up a JAVA socket server that is able to get everything from a html < form >. But when it comes to AJAX post, the server can only get the POST event, but it cannot read the "data" inside AJAX post. The following is the html code:

HTML

<!DOCTYPE html>
<head>
<meta charset="utf-8" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>

<script type="text/javascript">
$(document).ready(function() {
     $('#submit').click(function() {
//information to be sent to the server

info = $('#foo').val();
$.ajax({
  type: "POST",
  url: 'http://10.0.0.3:8888',
  data: ({foo: info}),
  //crossDomain: true,
  dataType: 'json'
});

return false;       
});

});
</script>

</head>
<body>

<label>Text</label>
<textarea id="foo"></textarea>

<button id="submit">Submit via Ajax</button>

</body>
</html>

I have no idea why this is happening, any suggestion?

Thank you

---------------------------------------------------------------------------------------------

Update

Java Server Code

    ServerSocket ss = new ServerSocket(8888);
    Socket s = ss.accept();

    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

    String inputLine;
    while (!(inputLine = in.readLine()).equals(""))
        System.out.println(inputLine);

    PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
    pw.println("aa");

    s.close();
    ss.close();

What I got at server is like:

POST / HTTP/1.1
Host: 10.109.3.184:8888
Connection: keep-alive
Content-Length: 58
Accept: */*
Origin: xxxxxxxxxxxxxxxxxx
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: xxxxxxxxxxxxxxxxxx
Accept-Encoding: gzip,deflate,sdch
Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4

The content does not appear...

POST data in HTTP request come as a request body, which is separated from head by empty line like this:

POST / HTTP/1.1
Host: 10.0.1.1:80
Connection: keep-alive
Content-Length: 29
Content-Type: text/json

{"id":123,"name":"something"}

So your server code should (more or less ) ;-) look like this:

BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));

String line;
List<String> headers = new LinkedList<>();
StringBuilder body = null;
while ((line = in.readLine()) != null) {
    //- Here we test if we've reach the body part.
    if (line.isEmpty() && body == null) {
        body = new StringBuilder();
        continue;
    }
    if (body != null) {
        body.append(line).append('\n');
    }
    else {
        headers.add(line);
    }
}

System.out.println("--- Headers ---");
for (String h : headers) {
    System.out.println(h);
}
System.out.println("--- Body ---");
System.out.println(body != null ? body.toString() : "");

Please note, that this code is only for test purposes. You can not assume, that body is a text (at least you should verify Content-Type header) and you can safely read it into StringBuilder or if it is safe at all to load it in whole into memory (at least you should verify Content-Length header). Despite of those headers you should expect the worst and during reading perform some sanity checks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM