簡體   English   中英

發送ajax請求到Netty HTTP服務器

[英]Send ajax request to Netty HTTP server

我嘗試從JavaScript向Netty 4 HTTP服務器發送異步請求,但未得到響應。 這是我使用的代碼。

Netty服務器初始化:

EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup(2);
try {
    ServerBootstrap b = new ServerBootstrap();
    b.option(ChannelOption.SO_BACKLOG, 1024);
    b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .childHandler(new HttpHelloWorldServerInitializer());

    Channel ch = b.bind(port).sync().channel();
    ch.closeFuture().sync();
} finally {
    bossGroup.shutdownGracefully();
    workerGroup.shutdownGracefully();
}

管道初始化-HttpHelloWorldServerInitializer

ChannelPipeline p = ch.pipeline();
p.addLast("logger", new LoggingHandler(LogLevel.INFO));
p.addLast("decoder", new HttpServerCodec());
p.addLast("handler", new HttpHelloWorldServerHandlerIn());

處理程序-HttpHelloWorldServerHandlerIn

public class HttpHelloWorldServerHandlerIn extends SimpleChannelInboundHandler<Object> {

    private static final byte[] CONTENT = { 'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd' };

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        if (msg instanceof HttpRequest) {
            FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.OK,
                    Unpooled.wrappedBuffer(CONTENT));
            response.headers().set(CONTENT_TYPE, "text/plain");
            response.headers().set(CONTENT_LENGTH, response.content().readableBytes());

            response.headers().set(CONNECTION, Values.KEEP_ALIVE);
            ctx.write(response);
        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
    }

}

HTML頁面

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Netty test</title>
<script type="text/javascript">
    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }


        xmlhttp.onreadystatechange = function() {
            document.getElementById("myDiv2").innerHTML = xmlhttp.readyState + " " + xmlhttp.status + " " + xmlhttp.getAllResponseHeaders();
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
            }

        }
        xmlhttp.open("GET", "http://localhost:8081/test", true);
        xmlhttp.send(null);
    }
</script>
</head>
<body>
<div id="myDiv"><h2>Text here</h2></div>
<div id="myDiv2"><h2></h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>

在HTML頁面中,我看到xmlhttp.readyState = 4,xmlhttp.status = 0,xmlhttp.getAllResponseHeaders()為空。

Netty日志顯示正確的轉儲我發送給JavaScript的內容

2013 4:15:19 PM io.netty.handler.logging.LoggingHandler logMessage
INFO: [id: 0x619b60f9, /0:0:0:0:0:0:0:1:59641 => /0:0:0:0:0:0:0:1:8081] WRITE(100B)
         +-------------------------------------------------+
         |  0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f |
+--------+-------------------------------------------------+----------------+
|00000000| 48 54 54 50 2f 31 2e 31 20 32 30 30 20 4f 4b 0d |HTTP/1.1 200 OK.|
|00000010| 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74 |.Content-Type: t|
|00000020| 65 78 74 2f 70 6c 61 69 6e 0d 0a 43 6f 6e 74 65 |ext/plain..Conte|
|00000030| 6e 74 2d 4c 65 6e 67 74 68 3a 20 31 31 0d 0a 43 |nt-Length: 11..C|
|00000040| 6f 6e 6e 65 63 74 69 6f 6e 3a 20 6b 65 65 70 2d |onnection: keep-|
|00000050| 61 6c 69 76 65 0d 0a 0d 0a 48 65 6c 6c 6f 20 57 |alive....Hello W|
|00000060| 6f 72 6c 64                                     |orld            |
+--------+-------------------------------------------------+----------------+

2013 4:15:19 PM io.netty.handler.logging.LoggingHandler flush
INFO: [id: 0x619b60f9, /0:0:0:0:0:0:0:1:59641 => /0:0:0:0:0:0:0:1:8081] FLUSH

請幫忙! 我做錯了什么?

我會用wireshark檢查一下,看這些字節是否真的是通過寫發送的。 另外,您還應檢查write(...)返回的ChannelFuture是否由於某種原因而失敗。

就像是:

ChannelFuture future = ctx.write(...);
future.addListener(new ChannelFutureListener() {
    public void operationComplete(ChannelFuture future) {
        System.out.println("success=" future.isSuccess());
        if (!future.isSuccess()) {
            future.cause().printStracktrace();
        }
    }
});

答案非常簡單...只需將Access-Control-Allow-Origin:*添加到響應標頭:

response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN, "*");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM