简体   繁体   English

读取Java Servlet中从Ajax发送的JQuery数据

[英]Reading JQuery data sent from Ajax in Java Servlet

Here is my Ajax code: 这是我的Ajax代码:

    var myJSONObject = {"bindings": [
                                     {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}]
                             };
    $.ajax({
        url : "ships",
        data : myJSONObject,
        success : function(data){
            GLOBAL.player.startShooting(data);
        },
        error : function(data) {
            console.log("error:", data);
        },
        dataType : "json",
        timeout : 30000,
        type : "post"
    });

And here is my Java Servlet code: 这是我的Java Servlet代码:

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    System.out.println("im in PSOT");
    System.out.println(request.getParameter("myJSONObject"));

    StringBuilder sb = new StringBuilder();
    BufferedReader br = request.getReader();
    String str;
    while( (str = br.readLine()) != null ){
        sb.append(str);
    }   
    System.out.println(sb.toString());
    response.setContentType("application/json");
    response.getWriter().write("{\"key\":\"hello\",\"key2\":\"world\"}");
}

The Java servlet returns my Hello World object, but i CANNOT read data in Java Servlet The console prints out the following: Java servlet返回我的Hello World对象,但我无法读取Java Servlet中的数据控制台打印出以下内容:

im in PSOT
null

The last line is an empty string from last println. 最后一行是来自上一个println的空字符串。

I am using Tomcat 7 我正在使用Tomcat 7

Can anyone please tell me what i am doing wrong and why i cannot read data in Java Servlet_ 谁能告诉我我做错了什么以及为什么我无法读取Java Servlet中的数据_

The parameter name is not myJSONObject . 参数名称不是myJSONObject That's the JS variable name. 那是JS变量名。 The parameter names are all the root keys which you have there in your JSON object. 参数名称是JSON对象中的所有根键。 Eg 例如

String bindings = request.getParameter("bindings");
// ...

You'd only need to manually parse it further. 您只需要进一步手动解析它。 You could use Google Gson for this. 您可以使用Google Gson

As to why the Reader didn't return anything, that's because the request body can be read and parsed only once . 至于为什么Reader没有返回任何内容,那是因为请求体只能被读取和解析一次 Any getParameter() call will implicitly do that. 任何getParameter()调用都会隐式执行此操作。 So when you call getParameter() before getReader() , you won't be able to read the request body by the Reader (the same applies for the other way round!). 所以当你 getReader() getParameter() 之前调用getParameter() ,你将无法通过Reader读取请求体(同样适用于其他方式!)。 But you don't need it anyway. 但无论如何你根本不需要它。 Just use getParameter() with the proper parameter names. 只需使用带有正确参数名称的getParameter()

You'd only need to manually parse it further. 您只需要进一步手动解析它。 You could use Google Gson for this. 您可以使用Google Gson。

As to why the Reader didn't return anything, that's because the request body can be read and parsed only once. 至于为什么Reader没有返回任何内容,那是因为请求体只能被读取和解析一次。 Any getParameter() call will implicitly do that. 任何getParameter()调用都会隐式执行此操作。 So when you call getParameter() before getReader(), you won't be able to read the request body by the Reader (the same applies for the other way round!). 所以当你在getReader()之前调用getParameter()时,你将无法通过Reader读取请求体(同样适用于其他方式!)。 But you don't need it anyway. 但无论如何你根本不需要它。 Just use getParameter() with the proper parameter names. 只需使用带有正确参数名称的getParameter()即可。

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

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