Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/428.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XMLHttpRequestJavaJavaScript_Java_Javascript_Xmlhttprequest - Fatal编程技术网

XMLHttpRequestJavaJavaScript

XMLHttpRequestJavaJavaScript,java,javascript,xmlhttprequest,Java,Javascript,Xmlhttprequest,我尝试在javascript和java之间进行通信。我的脚本javascript向java发送消息,java发送响应 javascript部分: var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4) { alert(xmlhttp.responseText);

我尝试在javascript和java之间进行通信。我的脚本javascript向java发送消息,java发送响应

javascript部分:

    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4)
        {
        alert(xmlhttp.responseText);
        }
      }
    var s = "LIGNE \n 2 \n il fait beau \nEND\n";
    xmlhttp.open("POST","http://localhost:6020",true);
    xmlhttp.send(s);
java部分:

    try {
        serverSocket = new ServerSocket(6020);
    } catch (IOException e) {
        System.err.println("Could not listen on port: 6020.");
        System.exit(-1);
    }
    serverSocket.accept()
    BufferedReader br = new BufferedReader(
                new InputStreamReader(socket.getInputStream()));
    BufferedWriter bw =  new BufferedWriter(
                new OutputStreamWriter(socket.getOutputStream()));
    String ligne = "";
    while(!(ligne = plec.readLine()).equals("END")){
        System.out.println(ligne);
    }
    bw.write("Il fait beau\n");
    bw.flush();
    bw.close();
    plec.close();
    socket.close();
输出java:

POST / HTTP/1.1
Host: localhost:6020
User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:13.0) Gecko/20100101 Firefox/13.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost:8080/test.html
Content-Length: 30
Content-Type: text/plain; charset=UTF-8
Origin: http://localhost:8080
Pragma: no-cache
Cache-Control: no-cache

LIGNE 
 2 
 il fait beau 
因此,我正确地接收到javascript发送的消息,但警报始终为空。如何回应此消息? 我尝试了很多可能性,但都不起作用。我不想使用servlet,这样做太麻烦了

谢谢

编辑:

我这样做:

bw.write("HTTP/1.1 200 OK\r\n"+
        "Content-Type: text/html; charset=utf-8\r\n"+
        "Content-Length: 13\r\n\r\n" +
        "il fait beau\n");
这是:

String data = "il fait beau \n";

StringBuilder builder = new StringBuilder();

builder.append("HTTP/1.1 200 OK\r\n");
builder.append("Content-Type: text/html; charset=utf-8\r\n");
builder.append("Content-Length:" + data.length() + "\r\n\r\n");
builder.append(data);
bw.write(builder.toString());

但警报仍然是空的。这可能是javascript中的一个问题。

javascript需要看到完整的HTTP响应。仅向其发送回字符,会使其放弃回复,因为它是无效的HTTP响应

在java代码中,返回如下内容

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: <length of data>

---data here---

javascript需要看到完整的HTTP响应。仅向其发送回字符,会使其放弃回复,因为它是无效的HTTP响应

在java代码中,返回如下内容

HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: <length of data>

---data here---
尝试:

HTTP头由
\r\n
(CRLF)分隔。标题和正文由
\r\n\r\n
标记

请注意,您将长度设置为13,因为您还必须计算字符串末尾的
\n

编辑:由于跨域策略,它无法工作<代码>http://localhost:6020与执行JavaScript的网站的端口不同,因此可能无法提供xmlhttprequest。

请尝试:

HTTP头由
\r\n
(CRLF)分隔。标题和正文由
\r\n\r\n
标记

请注意,您将长度设置为13,因为您还必须计算字符串末尾的
\n



编辑:由于跨域策略,它无法工作<代码>http://localhost:6020与执行JavaScript的网站的端口不同,因此xmlhttprequest可能无法交付。

如何构建完整的HTTP响应?您需要添加至少
HTTP/1.1 200 OK
,因为只有这样,JavaScript中的
readyState==4
,以及用于响应的
内容长度。(或者你发送了一个分块响应,但我想这对你来说太复杂了。)@Birk什么是分块响应?如果你发送
内容长度
,你必须在发送之前知道你的响应长度。如果您以前不知道,可以使用
Content-Transfer-Encoding:chunked
()如何构建完整的HTTP响应?您需要至少添加
HTTP/1.1200 OK
,因为只有这样,JavaScript中的
readyState==4
和响应的
Content-Length
。(或者你发送了一个分块响应,但我想这对你来说太复杂了。)@Birk什么是分块响应?如果你发送
内容长度
,你必须在发送之前知道你的响应长度。如果您以前不知道,可以使用
内容传输编码:chunked
()@DarkXphenomenon我编辑我的帖子。更新问题的
输出java:
部分,以显示发送新请求时发生的情况。@DarkXphenomenon输出没有更改。请尝试打开
http://localhost:6020
在浏览器中,来调试这个。如果您的回答正确,您应该会看到浏览器中显示的文本。您还可以使用firefox中的LiveHttpHeaders插件调试响应。另外,请注意对我的答案的编辑。将
内容类型
更改为
文本/纯文本
内容类型
不应该是问题所在…@DarkXphenomenon我编辑了我的帖子。更新问题的
输出java:
部分,以显示发送新请求时发生的情况。@DarkXphenomenon输出没有更改。请尝试打开
http://localhost:6020
在浏览器中,调试此操作。如果您的回答正确,您应该会看到浏览器中显示的文本。您还可以使用firefox中的LiveHttpHeaders插件调试响应。另外,请注意对我的答案的编辑。将
内容类型
更改为
文本/普通
内容类型
不应成为问题…谢谢,但警报中始终没有任何内容。抱歉,我的代码中当前有
内容长度:13\r\n\r\n+
。卸下
+
。这是打字错误。我在我的例子中改变了它。然后它应该会起作用。这不是问题,我移除了它,但总是一无所获。+1:D我没有想到@AnthonyMaia,您执行XMLHttp请求的站点是否也在本地主机上?如果是,它不能是同一个端口,因为他的Java程序已经在使用它。我也找不到它不起作用的另一个原因。但我最初也没有想到它^^^谢谢,但警报中始终没有任何内容。对不起,我的代码中当前有
内容长度:13\r\n\r\n+
。卸下
+
。这是打字错误。我在我的例子中改变了它。然后它应该会起作用。这不是问题,我移除了它,但总是一无所获。+1:D我没有想到@AnthonyMaia,您执行XMLHttp请求的站点是否也在本地主机上?如果是,它不能是同一个端口,因为他的Java程序已经在使用它。我也找不到它不起作用的另一个原因。但我一开始也没想到^^
bw.write("HTTP/1.1 200 OK\r\n"+
            "Content-Type: text/html; charset=utf-8\r\n"+
            "Content-Length: 13\r\n\r\n" +
            "il fait beau\n");