jQuery的Ajax和Java服务器,数据丢失数据丢失、服务器、jQuery、Ajax

2023-09-10 14:53:40 作者:笔尖微凉

我有这样的AJAX功能,看起来像这样

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);
     }

现在奇怪的是,当我送我的ajax我的服务器获取使用的System.out

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?

推荐答案

中的数据应该在标题行后的空白行之后,但我认为问题是,数据不与换行符结束,因此,你不能用 .readLine()方法读取它。

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.

虽然通过头线循环,可以查找内容长度行,得到的数据的长度。当你已经达到了空行,停止使用 .readLine()。代替切换到阅读一个字符的时间,则读取由内容长度头中指定的字符数。我想,你可以找到在这个答案。

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.

如果可以的话,我建议你使用一个库,以帮助这一点。我想的Apache HTTP核心库可以帮助这一点。请参见这个答案。

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.

 
精彩推荐
图片推荐