title: How to manipulate jQuery AJAX JSON data in a controller in spring MVC
body: <p>How many ways are to pass JSON data to a spring controller? </p>
  
  <p>I followed <a href="http://hmkcode.com/spring-undefined-mvc-json-to-java/" rel="nofollow">this tutorial</a> and they pass the data using the following syntax: </p>
  
  <pre><code>data: "{\"name\":\"hmkcode\",\"id\":2}",
  </code></pre>
  
  <p>This works but since I need to retrieve the data from a user using a text input I don't know how to put my variable in that string. </p>
  
  <p>I tried doing using the following syntax: </p>
  
  <pre><code>data: "{\"name\":\name\}" 
  </code></pre>
  
  <p>But it returns the following error: </p>
  
  <blockquote>
    <p>status: parsererror er:SyntaxError: Unexpected tokken a</p>
  </blockquote>
  
  <p>I have seen other sites that uses the following syntax: </p>
  
  <pre><code>data: {"name":name} 
  </code></pre>
  
  <p>But that gives me the same error.</p>
  
  <p>This works but I don't know if is the best approach.</p>
  
  <pre><code>var json = {"name" : name};
  ...
  data: JSON.stringify(json),
  </code></pre>
  
  <p>I manage to pass the JSON string to one of my controllers but I get the string like this:</p>
  
  <pre><code>{"name": Joe, "lastname": Smith} 
  </code></pre>
  
  <p>Is there a way to only get that info in a Person Object or at least get only Joe in a string and Smith in another one?</p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
          function doAjaxPost() 
          {
              // get the form values
              var name = $('#name').val();
              var lastname = $('#lastname').val();
  
              var json = {"name" : name, "lastname" : lastname};
              //console.log(json);
              $.ajax(
              {
                  type: "POST",
                  url: "formShow",
                  data: JSON.stringify(json), 
                  //data: "{\"name\":name}",
                  //data: {"name":name},
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  cache: false,
  
                  beforeSend: function(xhr) 
                              {
                                  xhr.setRequestHeader("Accept", "application/json");  
                                  xhr.setRequestHeader("Content-Type", "application/json");  
                              },
                  success: function(data) 
                          {
                              //console.log(data);    
                              console.log(data.name);
                              //var data = $.parseJSON(JSON.stringify(response));
                              //alert(data);
                              alert( "name: "+data.name);
                              //$('#name').val('');
                          },
                  error:function(data,status,er) { 
                      alert("error: "+data+" status: "+status+" er:"+er);
                  }
                  /* error: function (xhr, ajaxOptions, thrownError) 
                         {
                              alert(xhr.status);
                              alert(xhr.responseText);
                              alert(thrownError);
                  }*/
              });
          }
  &lt;/script&gt;
  
  &lt;fieldset&gt;
   &lt;legend&gt;Name in view&lt;/legend&gt;
          Name in view:   &lt;input type="text" id="name" name="name"&gt;
      &lt;br&gt;
      Last Name in view:   &lt;input type="text" id="lastname" name="lastname"&gt;
      &lt;br&gt;
  
     Show modify name in view:   &lt;input type="text" id="modifyname" name=""modifyname""&gt;
       &lt;br&gt;
      &lt;input type="button" value="Add Users" onclick="doAjaxPost()"&gt;
  &lt;/fieldset&gt;
   &lt;br&gt;
  </code></pre>
  
  <p>And these are my controllers:</p>
  
  <pre class="lang-js prettyprint-override"><code>@RequestMapping(value = "formShow", method = RequestMethod.GET)
      public String formularioIncidencia (Model model) {
          return "formShow";
      }
  
      @RequestMapping(value = "formShow", method = RequestMethod.POST)
      public @ResponseBody String getTags(@RequestBody String name) 
      {
  
          String variableAjax=  name;
          System.out.println("controller variable is  " + variableAjax);
          //that prints me this "{name: Joe, lastname: Smith}"
          return variableAjax;
      }
  </code></pre>
  
  <p>EDITED****
  this is my User class</p>
  
  <pre><code>public class Userimplements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      private String name;
      private String lastname;
  
      public User(){}
  }
  </code></pre>
  
  <p>I edited my controllers to the following </p>
  
  <pre class="lang-js prettyprint-override"><code>@RequestMapping(value = "formShow", method = RequestMethod.GET)
      public String formShow(Model model) {
          return "formShow";
      }
  
  @RequestMapping(value = "formShow", method = RequestMethod.POST)
  public @ResponseBody User getTags(@RequestBody final User user, Model model) 
  {
  
      //what should i do here parse my user to JSON how??
      user.setName("name changed");
      model.("modifyname", user.getName() ); 
      return User;
  }
  </code></pre>