简体   繁体   中英

Unable to access ajax data from servlet

I am trying to access the JSON Data sent from client in Java Servlet. I am unable to see the data.

My client Code:

// userObj - the user object 

$.ajax({
    type : 'POST',
    url : '/myServlet',
    success : function(result) {alert(result)},
    data : JSON.stringify(userObj),
});

At server side:

I am trying to read request.getParameterMap(), but I am unable to see the data.

Any help is greatly appreciated.

Use a JSON parser to parse the Servlet data if your AJAX requests sends the data properly.

import org.json.simple.JSONObject;

JSONObject jObj = new JSONObject(request.getParameter(<request_parameter_name>)); 
Iterator it = jObj.keys(); 
while(it.hasNext()) {
    //Key
    String key = it.next(); 
    //Value.
    Object value = jObj.get(key); 

}

This page lists all JSON parsers language-wise

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