0

I am not receiving the JSON data that I am sending from the AJAX request on JSP Page. When I try to print the array which stores the request.getParameterValues("json[]"), I get null pointer exception.

My code is below:

HTML:

<form name="form1" method="post" action="dailyWBSupload">
 <button class="btn btn-default btn-rect" type="submit" id="taskstodb">Forward</button>
</form>

jquery/AJAX Script:

   var form = $('#form1');
        form.submit(function() {
            var json = [1, 2, 3, 4];
            jQuery.ajax({
                type: "POST",
                url: "dailyWBSupload",
                dataType: 'json',
                data: {json: json},
                contentType: 'application/json',
                mimeType: 'application/json',
                success: function(data) {
                    alert('YES!');
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    alert(jqXHR + " - " + textStatus + " - " + errorThrown);
                }
            });
        });

web.xml:

 <servlet>
    <servlet-name>dailyWBSupload</servlet-name>
    <servlet-class>serves.dailyWBSupload</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>dailyWBSupload</servlet-name>
    <url-pattern>/dailyWBSupload</url-pattern>
</servlet-mapping>

My Servlet (dailyWBSupload.java):

public class dailyWBSupload extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //response.setContentType("text/html;charset=UTF-8");

    String[] myJsonData = request.getParameterValues("json[]");
    System.err.println("O/P: " + myJsonData[0]);


}

}

2 Answers2

0

Remove this two line from ajax call :

contentType: 'application/json',
mimeType: 'application/json',

OR change content type to :

contentType: 'application/x-www-form-urlencoded;'

Your ajax call after changes :

   $.ajax({
           type: "POST",
           url: "AnotherServlet",
           dataType: 'json',
           data: {json: json},         
           success: function(data) {
               alert('YES!');
           },
           error: function(jqXHR, textStatus, errorThrown) {
               alert(jqXHR + " - " + textStatus + " - " + errorThrown);
           }
       });

For more details see this answer.

Community
  • 1
  • 1
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50
0

Turns out, the method above is wrong, as didn't work a single time for me, even after making several adjustments to it. So I changed my approach, I used AJAX.updater() method to do the same.

Javascript:

 $("#taskstodb").click(function() {
                var table = $('#WBStable').tableToJSON(); // Convert the table into a javascript object
                alert(JSON.stringify(table));
                var json = JSON.stringify(table);
                new Ajax.Request('dailyWBSupload', {
                    method: 'get',
                    parameters: {json: json}
                });
            });

Servlet:

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String tabledata = request.getParameter("json");
    System.err.println(tabledata);
    final String JSON_DATA
            = "{" + "  \"geodata\": " + tabledata + "}";
    System.err.println(JSON_DATA);
    final JSONObject obj;
    String taskname;
    String noOfhours;
    try {
        obj = new JSONObject(JSON_DATA);
        final JSONArray geodata = obj.getJSONArray("geodata");
        final int n = geodata.length();
        for (int i = 0; i < n; ++i) {
            final JSONObject taskinfo = geodata.getJSONObject(i);
            taskname = taskinfo.getString("Task");
            noOfhours = taskinfo.getString("No. of Hours");

        }
    } catch (JSONException ex) {
        ex.printStackTrace();
    }

}