简体   繁体   中英

Send ajax request to Netty HTTP server

I try send async request from JavaScript to Netty 4 HTTP server and don't get response. Here is code what i use.

Netty server initialization:

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

Pipeline initialization - HttpHelloWorldServerInitializer

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

Handler - 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 page

<!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>

In HTML page i see xmlhttp.readyState=4, xmlhttp.status=0, xmlhttp.getAllResponseHeaders() is empty.

Netty log show correct dump what i send to 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            |
+--------+-------------------------------------------------+----------------+

and

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

Please help! What i doing wrong?

I would check with wireshark to see if the bytes are really send over the write. Also you should check if the ChannelFuture returned by write(...) was failed for some reason.

Something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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