I have a JSP page in which I am sending an AJAX Request to servlet having data as JSON Array. But I am getting null value in the servlet. Request.getParameter() returns null, but when i tried using a simple json object for e.g. data:{jsonData:'test'} it works for me.
Ajax Snippet
function updateStatus() {
var Url = {
"Urls" : [
{
"Env" : "DEV : CC",
"Url" : "https://localhost:8081/cc",
"Status" : "Up",
"Revision" : ""
}, {
"Env" : "MO : CM",
"Url" : "https://localhost:8082/ab",
"Status" : "Up",
"Revision" : "" } ] }
$.ajax({
url:'Environment',
data:{jsonData : Url},
type:'post',
cache:false,
success:function(data){
alert('Hi');
},
error:function(){
alert('error');
}
});
}
Servlet function
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try
{
String jsonString = request.getParameter("jsonData");
JSONArray array = new JSONArray(jsonString);
for (int i = 0; i < array.length(); i++) {
String path = array.getString(i);
System.out.println(path);
}
}
catch (JSONException e)
{
e.printStackTrace();
}