title: JSON, post using jquery ajax is returning fail
body: <p>I have below code, In this I want to convert the entire form to JSON and post it using jquery AJAX. My problem is, its getting into the Servlet and I can get the values in request.getParameter, but still I am getting ajax fail. Once I will get the response I want to display the returned response and display it on the same page. Please help me finding what is wrong. I have searched a lot but couldnt link it to right ans. Many Many Thanks in Advance!!</p>
  
  <p>Here my code. ShowHideDiv_ajax.html</p>
  
  <pre><code>&lt;script&gt;
    $(document).ready(function() {
      $('#form_submit').click(function (event) {
        event.preventDefault();
        var form = $("#myform");
        var json = ConvertFormToJSON(form);
        $("#results").text(JSON.stringify(json)  );
  
        $.ajax({
          url: 'AjaxServlet',
          type: 'POST',
          dataType: 'json',
          cache: false,
          //contentType: 'application/json; charset=utf-8',
          data: json,
          success: function( response ) {
            //I want to use this response  to be displayed on the same page.
            alert('success');
          },
          error: function() { // if error occured
            alert('fail:');
          }
        });
  
        return false;
      });
  
      function ConvertFormToJSON(form){
        var array = form.serializeArray();
        var json = {};
        $.each(array, function() {
          //alert('this.name='+this.name+'this.value='+this.value);
          if (json[this.name] !== undefined) {
            if (!json[this.name].push) {
              json[this.name] = [json[this.name]];
            }
            jsono[this.name].push(this.value || '');
          } else {
            json[this.name] = this.value || '';
          }
        });
        return json;
      }
     });     
  &lt;/script&gt;
  &lt;style&gt;
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form class="ajax_form" id="myform" name="myform" method="post" action="AjaxServlet" &gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;td colspan="2"&gt;&lt;div id="error" class="error"&gt;&lt;/div&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;Enter your name : &lt;/td&gt;
          &lt;td&gt; &lt;input type="text" id="name" name="firstname"&gt;&lt;br/&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td&gt;Education : &lt;/td&gt;
          &lt;td&gt; &lt;input type="text" id="education" name="edu"&gt;&lt;br/&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
          &lt;td colspan="2"&gt;&lt;div id="info" class="success"&gt;&lt;/div&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/form&gt;
    &lt;p&gt;&lt;tt id="results"&gt;&lt;/tt&gt;&lt;/p&gt;
    &lt;p&gt;&lt;tt id="results1"&gt;&lt;/tt&gt;&lt;/p&gt;
    &lt;input class="ajax_button" type="submit" value="Submit"  id="form_submit" name="form_submit"&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>And Servelt AjaxServlet.java:</p>
  
  <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
    // TODO Auto-generated method stub
    System.out.println("in post ajaxservlet");
    try {
      String fn, ed=null;
      fn = request.getParameter("firstname");
      ed = request.getParameter("edu");
      System.out.println("receieved data:"+fn+ed);
      if(request.getParameter("firstname").toString()!=null){
        fn="Hello User";
      }
  
      PrintWriter out = response.getWriter();
      response.setContentType("text/json");
      response.setCharacterEncoding("UTF-8"); 
      out.write(fn);
      out.close();
      System.out.println("data posted");
    } catch (Exception e) {
      // TODO: handle exception
      e.printStackTrace();
    }
  }
  </code></pre>