Java 无法在play 1.2.5中获取json?

Java 无法在play 1.2.5中获取json?,java,javascript,json,playframework,playframework-1.x,Java,Javascript,Json,Playframework,Playframework 1.x,我正在使用play 1.2.5。我通过ajax从一个视图发送一个JSON对象,但是我的控制器中出现了一个空指针异常。下面是我的代码。它从Html字段中获取值并将其发送到播放控制器 查看 function submitData(){ 'use strict'; var user = document.getElementById('user').value; var pass = document.ge

我正在使用play 1.2.5。我通过ajax从一个视图发送一个JSON对象,但是我的控制器中出现了一个空指针异常。下面是我的代码。它从Html字段中获取值并将其发送到播放控制器

查看

function submitData(){
                'use strict';
                var user = document.getElementById('user').value;
                var pass = document.getElementById('pass').value;

                var message = "";
                if(user == "" && pass==""){
                    //message=null;
                    message = "Please Enter User and Password";
                    document.getElementById('submitb').disabled=true;
                }else if(user === ""){
                    //message= null;
                    message = " Please Enter User name";
                    document.getElementById('submitb').disabled=true;
                }else if(pass == ""){
                    //message=null;
                    message = "Please Enter Password";
                    document.getElementById('submitb').disabled=true;
                }

                if(message!=""){
                    document.getElementById('result').innerHTML = message;
                    document.getElementById('submitb').disabled= false;
                    message = null;
                }else{
                    sendAjax();
                }


            } // ending submit function
SendAjax函数:

function sendAjax(){

                'use strict';
                var ajax = getXhrObject();
                ajax.onreadystatechange = handleAjaxResponse;
                    //var userData =  
                    var data = {'user': document.getElementById('user').value,'pass': document.getElementById('pass').value}
                    var jsonData = JSON.stringify(data);

                    ajax.open('POST', '/validate', true);
                    ajax.setRequestHeader('contentType', 'application/json');
                    var ajaxAbortTimer = setTimeout(function() { //abort timer
                        if (ajax) {
                                ajax.abort();
                                ajax = null;
                        } // second 'if (ajax)' ends here
                    }, 5000);
                    ajax.send(jsonData);







            }// ending sendAjax function.
getXhrObject()

控制器: 这是我的控制器的代码。当我向它发送请求时,会引发空指针异常

public static void validate(Request request){
        String status = null, response = null;
        List<String> credInfo = new ArrayList<String>();
        if (request.contentType.equals("application/json")){
            Iterator<String> it = request.params.allSimple().keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = request.params.get(key);
                Logger.info("value" + value);
                JsonElement body = new JsonParser().parse(value);
                JsonObject jsObj = body.getAsJsonObject();
                JsonElement JsonEmail = jsObj.get("user");
                JsonElement JsonPassword = jsObj.get("pass");
                String email = JsonEmail.getAsString();
                String password = JsonPassword.getAsString();
                System.out.println("email:  "+email);
                System.out.println("password:"+password);
                if(!email.equals(user) && !password.equals(pass)){

                status = "fail"; response = "Authentication failed";

            }
        }

    }
        renderJSON("{\"status\":"+status+" \response\":"+response+" }");

  }

您使用play framework的方式不正确:

控制器方法参数与您发送的参数名称匹配。http请求不是一个参数,因此不能使用这样的参数。请求对象已定义。尝试从方法中删除此参数


另一个问题是,您在ajax调用中没有为json对象命名,因此无法使用request.params.get(“user”)之类的东西检索它。

您认为您的JS第2行有一个类型,“use strick”应该是“use strict”,对吗?那个sendAjax()函数是什么?哦,对不起,它是“严格使用”,谢谢你指出那个错误。我在问题中添加了sendAjax函数。
public static void validate(Request request){
        String status = null, response = null;
        List<String> credInfo = new ArrayList<String>();
        if (request.contentType.equals("application/json")){
            Iterator<String> it = request.params.allSimple().keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                String value = request.params.get(key);
                Logger.info("value" + value);
                JsonElement body = new JsonParser().parse(value);
                JsonObject jsObj = body.getAsJsonObject();
                JsonElement JsonEmail = jsObj.get("user");
                JsonElement JsonPassword = jsObj.get("pass");
                String email = JsonEmail.getAsString();
                String password = JsonPassword.getAsString();
                System.out.println("email:  "+email);
                System.out.println("password:"+password);
                if(!email.equals(user) && !password.equals(pass)){

                status = "fail"; response = "Authentication failed";

            }
        }

    }
        renderJSON("{\"status\":"+status+" \response\":"+response+" }");

  }
Internal Server Error (500) for request POST /validate

Execution exception (In /app/controllers/Application.java around line 30)
NullPointerException occured : Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)

play.exceptions.JavaExecutionException: Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:237)
    at Invocation.HTTP Request(Play!)
Caused by: java.lang.NullPointerException: Try to read contentType on null object play.mvc.Http$Request (controllers.Application.validate, line 30)
    at play.classloading.enhancers.PropertiesEnhancer$FieldAccessor.invokeReadProperty(PropertiesEnhancer.java:213)
    at controllers.Application.validate(Application.java:30)
    at play.mvc.ActionInvoker.invokeWithContinuation(ActionInvoker.java:557)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:508)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:484)
    at play.mvc.ActionInvoker.invokeControllerMethod(ActionInvoker.java:479)
    at play.mvc.ActionInvoker.invoke(ActionInvoker.java:161)
    ... 1 more