1

Spring Controller Method :

    @RequestMapping(value="/checklist/{id}",method=RequestMethod.PUT, consumes=MediaType.APPLICATION_JSON_VALUE , produces=MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Checklist update(@RequestBody Checklist checklist, @PathVariable("id") int id)
    {
        checklist.setId(id);
        return service.update(checklist);

    }

JavaScript AJAX code:

    var checklist={name:$('#newName').val(), details:$('#newDetails').val()};
                    $.ajax({                //send updated item values to
                        method:'put',
                        url:'/tracker/checklist/'+$(editingItem).attr('id'),
                        contentType:'application/json',
                        dataType:'json',
                        data:checklist,
                        success:function(data)
                        {
                            console.log(data);
                            $('#myModal').modal('hide');
                        }
                    });

Checklist Model:

package com.tracker.web.models;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

@Entity
@Table(name="checklists")
public class Checklist {

    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    private int id;

    private int item_order;
    private String name;
    private String details;
    private String phase;
    private String completed;
    private String skipped_note;
    private Date completed_on;
    private int completed_by;

    @Temporal(TemporalType.TIMESTAMP)
    @CreationTimestamp
    private Date created_at;

    @Temporal(TemporalType.TIMESTAMP)
    @UpdateTimestamp
    private Date updated_at;

    @ManyToOne
    private Event event;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getItem_order() {
        return item_order;
    }

    public void setItem_order(int item_order) {
        this.item_order = item_order;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDetails() {
        return details;
    }

    public void setDetails(String details) {
        this.details = details;
    }

    public String getPhase() {
        return phase;
    }

    public void setPhase(String phase) {
        this.phase = phase;
    }

    public String getCompleted() {
        return completed;
    }

    public void setCompleted(String completed) {
        this.completed = completed;
    }

    public String getSkipped_note() {
        return skipped_note;
    }

    public void setSkipped_note(String skipped_note) {
        this.skipped_note = skipped_note;
    }

    public Date getCompleted_on() {
        return completed_on;
    }

    public void setCompleted_on(Date completed_on) {
        this.completed_on = completed_on;
    }

    public int getCompleted_by() {
        return completed_by;
    }

    public void setCompleted_by(int completed_by) {
        this.completed_by = completed_by;
    }

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at() {
        this.created_at = new Date();
    }

    public Date getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at() {
        this.updated_at = new Date();
    }

    public Event getEvent() {
        return event;
    }

    public void setEvent(Event event) {
        this.event = event;
    }

}

I am using Jquery 1.11. when i use with 'GET' instead of 'PUT' 'method' on client side and 'consumes' on server side, it works. even i tried with JSON.stringify while sending sending. i am using jackson on server side to convert data into json

phani
  • 3,123
  • 7
  • 33
  • 62
  • You are not passing "checklist" parameter from AJAX and it is mandatory parameter in your controller method. Either mark the "checklist" as nullable or pass it with the correct name from your query – Utkarsh Jul 24 '15 at 05:06
  • do you mean data from ajax. data:{"name":"item1"}. i want to map this to Checklist model. or should i pass like this-- checklist={"name":"item1"}; data:checklist; – phani Jul 24 '15 at 05:33
  • Use String as datatype (of checklist) and then convert the json to Java using jackson from this String or Refer this http://stackoverflow.com/questions/12893566/passing-multple-variables-in-requestbody-to-a-spring-mvc-controller-using-ajax – Utkarsh Jul 24 '15 at 05:50
  • Please post your CheckList class – Utkarsh Jul 24 '15 at 05:55
  • Can you ellaborate on what you mean with "when I use with 'GET' instead of 'PUT'" it works. Do you mean changing the `method: 'put'` property in jQuery or changing `method=RequestMethod.PUT`? – g00glen00b Jul 24 '15 at 08:18
  • updated with complete code – phani Jul 24 '15 at 13:02

1 Answers1

1

Which version of jquery you are using?

If you are using jquery prior to 1.9.0 than try type: 'PUT' instead of method:'put' in your ajax call and it should work. Otherwise it should be of with method:'put' .

Check documentation for more reference http://api.jquery.com/jquery.ajax/

commit
  • 4,607
  • 14
  • 38
  • 68