繁体   English   中英

AJAX:POST方法的内容未传输

[英]AJAX: POST method content is not transferred

我是Client Server应用程序的初学者。 在阅读了几个论坛之后,我使用Java Server开发了一个基本的Javascript Client,并尝试使用POST发送数据。 但是,当我成功建立它们之间的连接时,在服务器端打印接收到的数据时,它仅打印标题,而不打印实际数据内容。 像这样

The Client /127.0.0.1:34290 is connected
The HTTP request string is ....
POST / HTTP/1.1
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.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: 30
Origin: null
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

客户端代码是:

$(document).ready(function(){
    $("button").click(function(){
        $.post("demo_test_post.asp",
        {
          name: "Donald Duck",
          city: "Duckburg"
        },
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});

服务器端代码为:

inFromClient = new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));
outToClient = new DataOutputStream(connectedClient.getOutputStream());

String requestString = inFromClient.readLine();
String headerLine = requestString;

StringBuffer responseBuffer = new StringBuffer();
responseBuffer.append("<b> This is the HTTP Server Home Page.... </b><BR>");
responseBuffer.append("The HTTP Client request is ....<BR>");

System.out.println("The HTTP request string is ....");
while (inFromClient.ready()) {
// Read the HTTP complete HTTP Query
    responseBuffer.append(requestString + "<BR>");
    System.out.println(requestString);
    requestString = inFromClient.readLine();
}

谁能告诉我我要去哪里错了?

看来您的服务器代码没有打印请求中的最后一行(它通过inFromClient.readLine()读取它,然后inFromClient.ready()切换为false,并且忘记了最后一行而不进行打印)。

尝试以下操作(没有响应内容,仅用于验证请求是否完整):

inFromClient = new BufferedReader(new InputStreamReader(connectedClient.getInputStream()));

String requestString;
while ((requestString = inFromClient.readLine()) != null) {
    System.out.println(requestString);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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