Hussy
Hussy

Reputation: 2029

How to process Json using jquery /javascript

Hi my setDefaultPoint controller returns map as a json... the code is below

def setDefaultPoint = {
    if (springSecurityService.isLoggedIn()) {
        def officeId = params.officeId          
        def officeRoleInstance=OfficeRole.get(officeId)
        def company= officeRoleInstance.company
        def defaultPoint = officeRoleInstance.distanceChart.officePoint
        def map = [defaultPoint:defaultPoint]
        map << [company:company]
        def json = map as JSON
        render json
    }

the ajax call which sends to request to this controller is

function setDefaultPoint(){
        var officeId =  $('#clientTrip_officeId').val();

        $.ajax({
            url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
            dataType: "json",
            data:({officeId: officeId}),
            success: function(data) {

                // here i want to get the id and name of default point and id and name of company...
        console.log('id of defaultpoint is---"+????) 
                    console.log('name of default point is---"+????)
                    console.log(id of company is-----------"+?????)        
            }

        }); 
    }

from json i am passing two different object.. so how to get the propirties of those object... both defaultPoint object and Company object has fields called id and anme

the response is looks like

 {"defaultPoint":{"class":"com.springpeople.steer.trips.Point","id":3,"name":"MG road"},"company":{"class":"com.springpeople.steer.partymodel.parties.Company","id":5,"addressPermanent":{"class":"Address","id":3},"contactDetails":null,"name":"Infosys","offices":[{"class":"OfficeRole","id":6}],"organizationRoles":[{"class":"OrganizationRole","id":5}],"panNumber":null,"serviceTaxNumber":null,"tanNumber":null}}

Upvotes: 1

Views: 5019

Answers (4)

Richard
Richard

Reputation: 22036

Because you have specified the dataType to be Json jQuery will parse the response into an object so you should be able to use:

function setDefaultPoint(){
        var officeId =  $('#clientTrip_officeId').val();

    $.ajax({
        url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
        dataType: "json",
        data:({officeId: officeId}),
        success: function(data) {

            // here i want to get the id and name of default point and id and name of company...
    console.log(data.defaultPoint.Id)         
        }

    }); 
}

Upvotes: 1

Russ Cam
Russ Cam

Reputation: 125538

Since the returned dataType is specified as json, the data argument passed to the function assigned to success will be the JavaScript object parsed from the returned json string, so long as the string is valid json. the data passed in the ajax call doesn't need the parens around it. This should work

function setDefaultPoint(){
    var officeId =  $('#clientTrip_officeId').val();

    $.ajax({
        url:"${createLink(controller:'clientTrip',action:'setDefaultPoint')}",
        dataType: "json",
        data: { officeId: officeId },
        success: function(data) {
            console.log(data.defaultPoint.id, data.defaultPoint.name);
            console.log(data.company.id, data.company.name);
        }

    });
}

Upvotes: 4

Rafay
Rafay

Reputation: 31043

if you have set dataType: "json" you dont need to parseJSON simply in your success callback do

alert(data.class);

Upvotes: 1

user888750
user888750

Reputation:

var obj = jQuery.parseJSON(data);

then you can access things like obj.class and such.

DOCUMENTATION: http://api.jquery.com/jQuery.parseJSON/

Upvotes: 1

Related Questions