1

I am working on a Spring MVC application in which I need to show details of a Trip object. Following is my TripModel:

@Entity
@Table(name="Trip")
public class TripModel {
    private String locationName;

    private String userName;
    @Id
    @Column(name="tripid")
    @GeneratedValue 
    private int tripId; 

    @Column(name="tripid")
    private int tripId;

    @Column(name="locationid")
    private int tripStopLocationId;

    @Column(name="datetime")
    private String tripStopDateTime;

    @Column(name="createts")
    private Date tripStopCreateTime;

    @Column(name="userid")
    private int createUserId;

    @Transient
    private List<ItemTransactionModel> itemTransactionModelList;
}

How can I get trip details using trip id, in AJAX? TripModel has a list of ItemTransactionModel objects.

Following is my ajax code:

jQuery.ajax({
        url: '<c:url value="/trip/tripdetailsbyajax" />',
        type: 'POST',
        data: "tripId="+tripId,
        cache:false,
        success:function(response){
            alert("response = " + response);
        },
        error:function(jqXhr, textStatus, errorThrown){
            alert(jqXhr);
            alert(textStatus);
            alert(errorThrown);
        }
    });

Following is my controller method:

@RequestMapping(value = "/tripdetailsbyajax", method = { RequestMethod.POST })
    public @ResponseBody TripModel getTripDetailsByAjax(int tripId) {
        TripModel tripModel = null;
        tripModel = tripService.getTripModel(tripId);
        return tripModel;
    }

How can we send TripModel object as AJAX response, and how can we access that in JavaScript? TripModel object has a list of ItemTransactionModel objects.

halfer
  • 19,824
  • 17
  • 99
  • 186
Vishal Suri
  • 447
  • 2
  • 12
  • 32

1 Answers1

2

You need to make a few changes to get your response as JSON,

Add a dataType:json to your ajax request so that it knows that it should expect the JSON from server

jQuery.ajax({
        url: '<c:url value="/trip/tripdetailsbyajax" />',
        type: 'POST',
        dataType: 'json',

Add a produces="application/json" in the RequestMapping of your handler method. This will hint the representation to which the framework will convert the response

@RequestMapping(value = "/tripdetailsbyajax", method = { RequestMethod.POST }, produces="application/json")

Finally, access the values in your success method like response.userName etc

Note, make sure that you're entity has proper getters/setters, the jackson libraries that are in charge of converting your object to JSON, work on properties

Master Slave
  • 27,771
  • 4
  • 57
  • 55
  • Thanks. But how can we iterate ItemTransactionModel list and get ItemTransactionModel object attributes? – Vishal Suri Feb 04 '15 at 10:16
  • the object will be returned as JSON, so you would have a JSON collection response.itemTransactionModelList that you should iterate through like any other JSON collection, take a look at http://stackoverflow.com/questions/1078118/how-do-i-iterate-over-a-json-structure, just make sure to provide getters and setters for both the transient list as well as for the fields inside the ItemTransactionModel – Master Slave Feb 04 '15 at 10:20