title: Passing parameter to Controller by using ajax
body: <p>Used Spring boot, MySQL, JPA and RESTFUL CRUD API.</p>
  
  <p>What I'm trying to do is passing the parameter to Controller so that the parameter goes into the table. But null values go into the table and I can't figure out which part is the problem. Only I can sure about is that the Controller doesn't receive the appropriate value. </p>
  
  <p>Under below, I posted Entity class(Status.java), Controller(GuideController.java) and html form(guide.html).</p>
  
  <p>Status.java</p>
  
  <pre><code>@Entity
  @Table(name="tbl_status")
  public class Status {
  
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  
  private Integer id;
  
  private String xml_to_json;
  
  
  public String getXml_to_json() {
      return xml_to_json;
  }
  
  public void setXml_to_json(String xml_to_json) {
      this.xml_to_json = xml_to_json;
  }
  </code></pre>
  
  <p>GuideController.java</p>
  
  <pre><code>    @Autowired
  private StatusRepository statusRepository;
  
  @GetMapping(path="/insert") // Map ONLY GET Requests
  public @ResponseBody String addNewStatus (@RequestParam String xml_to_json
          , @RequestParam String json_to_xml) {
      // @ResponseBody means the returned String is the response, not a view name
      // @RequestParam means it is a parameter from the GET or POST request
  
      Status s = new Status();
      s.setXml_to_json(xml_to_json);
      //s.setJson_to_xml(json_to_xml);
      statusRepository.save(s);
      return "Saved";
  }
  
  @GetMapping(path="/all")
  public @ResponseBody Iterable&lt;Status&gt; getAllUsers() {
      // This returns a JSON or XML with the users
      return statusRepository.findAll();
  }
  </code></pre>
  
  <p>guide.html</p>
  
  <pre><code>var xml2json = function(_data){
  $.ajax({   
      type: "POST",   
      url: url + "xml2json",   
      data: _data,
      contentType : "application/xml",
      cache: false,
      success : function(data) {   
          $("#output-xml-to-json").html(JSON.stringify(data,null,4));
          $.ajax({
              type:"POST",
              url: url + "insert",
              data: {xml_to_json:"success"},
              contentType: "text"
          })
      },
      error : function(){
          $("#output-xml-to-json").html("xml2json error");
          $.ajax({
              type:"POST",
              url: url + "insert",
              data: {xml_to_json:"error"},
              contentType: "text"
          })
      }
  });
  </code></pre>
  
  <p>}</p>