无法让json参与比赛1.2.5?

时间:2012-10-19 08:17:50

标签: java javascript json playframework playframework-1.x

我正在使用游戏1.2.5。我通过ajax从视图发送JSON对象,但是我的控制器中出现了Null Pointer Exception。以下是我的代码。它从Html字段获取值并将它们发送到Play控制器。

查看

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

function getXhrObject(){
                'use strict';
                var ajax = null; 
                try{
                    if(window.XMLHttpRequest){
                        ajax = new XMLHttpRequest();
                    }else if(window.ActiveXObject){
                        ajax = new ActiveXObject();
                    }



                }catch(e){
                    console.log(e);
                }

                return ajax;

            } // ending getXhrObject Function

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

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

1 个答案:

答案 0 :(得分:2)

您没有以正确的方式使用Play框架:http://www.playframework.org/documentation/1.2.5/controllers#params

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

另一个问题是你没有在你的ajax调用中为你的json对象命名,所以你不能用request.params.get(&#34; user&#34;)