jquery ajax and java server, lost data

一曲冷凌霜 提交于 2019-12-04 06:26:08

问题


i have this ajax function that looks like so

   $.ajax({
                      type: "POST",
                      url: "http://localhost:55556",
                      data: "lots and lots of pie",
                      cache: false,
                      success: function(result)
                      {     
                        alert("sent");


                      },
                      failure: function()
                      {
                              alert('An Error has occured, please try again.');
                      }
              });

and a server that looks like so

 clientSocket = AcceptConnection();

 inp = new BufferedReader(new InputStreamReader (clientSocket.getInputStream()));
 String requestString = inp.readLine();

 BufferedReader ed = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

   while(true){
          String tmp = inp.readLine();
          System.out.println(tmp);
     }

now the odd thing is when i send my ajax my server gets by using system.out

Host: localhost:55556
User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Content-Length: 20
Origin: null
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache  

the question is where is the data that i sent through, where is lots of pie?


回答1:


The data should come after a blank line after the header lines, but I think the problem is that the data does not end with a newline character, and therefore, you cannot read it with the .readLine() method.

While looping through the header lines, you could look for the "Content-Length" line and get the length of the data. When you have reached the blank line, stop using .readLine(). Instead switch to reading one character at a time, reading the number of characters specified by the "Content-Length" header. I think you can find example code for this in this answer.

If you can, I suggest you use a library to help with this. I think the Apache HTTP Core library can help with this. See this answer.



来源:https://stackoverflow.com/questions/22443146/jquery-ajax-and-java-server-lost-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!