简体   繁体   中英

Send JSON by ajax and get parameters by request in JSP

I need to send a JSON object by ajax (with Jquery) and get all parameters by the request Object in JSP (server side).

My JS code is:

        var request = new Object();
        request.param1= "value1";
        request.param2 = "value2";
        $.ajax({
        type:'GET',
        url: 'test.jsp',
        //data: {request:JSON.stringify(dataSend)},
        //data: {request:dataSend},
        //data: JSON.stringify(request),
        data:request,
        async:true,
        success:function(r){
            console.log(r);
        },
        error:function(error){
            console.log(error);
        }
    });

And my JSP code is:

<%@page import="cl.test.pos.web.delegate.POSXXXXXXXX"%>
<%@page import="org.json.simple.JSONObject"%>
<% 
JSONObject j = new JSONObject();
if(session.getAttribute("role") != null ){
    POSXXXXXXXX bx = new POSXXXXXXXX();
    String je;
    je = bx.setTest(request);
    out.print(je);
    out.close();
}else{
    j.put("responseStatus","EXCEPTION");
    request.getSession().invalidate(); 
    out.print(j);
    out.close();
}
%>

And the Method Class is

    public String setTest(HttpServletRequest request) throws IOException{
    JSONObject j = new JSONObject();
    try{
        j.putAll(request.getParameterMap());
        j.put("responseStatus", "OK");
    }catch(FrameworkException e){
        /*Any code*/
    }catch(Throwable t){
        /*Any code*/
    }
    return j.toJSONString();
    }

I Expect return a JSON Object on client and this is so, but, the response is like this:

{"param1":[Ljava.lang.String;@182f12f,"param2":[Ljava.lang.String;@1a881f5}

Values are not understandable and if I send Objects and Arrays, it are so wrong too, for example:

{"parametro4[1][p3]":[Ljava.lang.String;@c5954b,"parametro4[1][p4]":[Ljava.lang.String;@1cc9339,"parametro5[arr1][]":[Ljava.lang.String;@1d5af30}

Please Help me to get All parameters on a JSONObject from HttpServletRequest. I really need to know the best way to do this.

(I already searched in StackOverFlow and the surfing in the web, and I cannot found The best way to do this).

The parameterMap value is an Array Object and not String:

Returns: an immutable java.util.Map containing parameter names as keys and parameter values as map values. The keys in the parameter map are of type String. The values in the parameter map are of type String array .

javadoc for getParameterMao

So you will need to code it, just iterate throught the map and put the parameter name/value in the object.

I already resolved this problem (almost at all), from this way:

public String setTest(HttpServletRequest request) throws IOException{
JSONObject j = new JSONObject();
try{
    JsonParser jp = new JsonParser();
    Map m = request.getParameterMap();
    Gson gi = new Gson();
    String stringJson = gi.toJson(m);
    j.put("jsonParse",jp.parse(stringJson));
    j.put("responseStatus", "OK");
}catch(FrameworkException e){
    /*Any code*/
}catch(Throwable t){
    /*Any code*/
}
return j.toJSONString();
}

and server response correctly (almost):

{"jsonParse":{"parametro5[arr1][]":["1","2","3"],"pagoAbono":["false"],"parametro4[1][p4]":["3"],"parametro4[1][p3]":["2"],"parametro1":["parametro1"],"parametro4[0][p2]":["1"],"codigoCaja":[""],"parametro5[arr2][1][0][letraX]":["x"],"numeroCheque":[""],"facturasPagos":["195310|234509"],"rutCliente":["154809597"],"banco":[""],"caducidadMes":[""],"parametro4[0][p1]":["0"],"parametro5[arr2][1][]":["x","x"],"numeroTarjeta":[""],"caducidadYear":[""],"montoTotalPago":["334772"],"nombreTitular":[""],"parametro5[arr2][0][]":["a","b","c"],"parametro3[]":["a","b","c"],"parametro2[atributo1]":["atributo1"]},"responseStatus":"OK"}

The only Detail is that the Arrays and Objects are interpreted like the request parameter instead the real Javascript JSONObject.

For example:

If client send this:

var obj = new Object();
obj.param1 = [1,2,3];

Server response this:

obj[param1][0]=1;
obj[param1][1]=2;
obj[param1][2]=3;

Instead to response:

obj.param1[0]=1;
obj.param1[1]=2;
obj.param1[2]=3;

I hope you can understand the problem, and give to me a clue or solution.

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