【问题标题】:could not get json in play 1.2.5?无法在 play 1.2.5 中获取 json?
【发布时间】:2012-10-19 08:17:49
【问题描述】:

我正在使用 play 1.2.5。我正在通过 ajax 从视图发送 JSON 对象,但在控制器中出现空指针异常。下面是我的代码。它从 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

【问题讨论】:

  • 认为你在 JS 的第 2 行有一个类型,'use strick' 应该是 'use strict' 对吗?那 sendAjax() 函数是什么?
  • 哦,对不起,这是“使用严格”感谢您指出这个错误。
  • 我在我的问题中添加了 sendajax 函数。

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


【解决方案1】:

你没有正确使用游戏框架:http://www.playframework.org/documentation/1.2.5/controllers#params

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多