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.

1
  • what is the datatype(JSON/XML ....) that you receive from Spring service? Commented Feb 4, 2015 at 9:47

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. But how can we iterate ItemTransactionModel list and get ItemTransactionModel object attributes?
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 stackoverflow.com/questions/1078118/…, just make sure to provide getters and setters for both the transient list as well as for the fields inside the ItemTransactionModel

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.