I am tring to send a json string from js to servlet. I use ajax jquery to post to the servlet.
I can see the servlet post getting called but an exception occurs when I retrieve the json string in java, what am I missing
Javascript side:
var jsonText = JSON.stringify(SomeString);
$.ajax({
dataType: 'json',
url: fullpath,
type: 'POST',
data: jsonText
});
Java side:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import com.json.JSONException;
import com.json.JSONObject;
import com.json.JSONArray;
In post
response.setContentType("application/json");
JSONObject jObj = new JSONObject(request.getParameter("jsonText"));//Null pointer exception
Error:
java.lang.NullPointerException
at java.io.StringReader.<init>(StringReader.java:33)
at com.json.JSONTokener.<init>(JSONTokener.java:84)
at com.json.JSONObject.<init>(JSONObject.java:314)
JSON string:
{ "CustomerDetails": [{'name':'Amy','age':'23'},{'name':'Amj','age':'25'},{'name':'Amg','age':'27'}]}