有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

XMLHttpRequestJavaJavaScript

我尝试在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中的一个问题


共 (2) 个答案

  1. # 1 楼答案

    尝试:

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

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

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

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

  2. # 2 楼答案

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

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

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

    Reference

    类似于:

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