开发者

How to process Json using jquery /javascript

开发者 https://www.devze.com 2023-03-28 11:09 出处:网络
Hi my setDefaultPoint controller returns map as a json... the code is below def setDefaultPoint = { if (springSecurityService.isLoggedIn()) {

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 o开发者_如何学运维fficeId =  $('#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}}


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);
        }

    });
}


var obj = jQuery.parseJSON(data);

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

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


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

alert(data.class);


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)         
        }

    }); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消