如何使用jQuery / JavaScript的处理JSON如何使用、jQuery、JSON、JavaScript

2023-09-10 21:25:45 作者:想要陪你到最后

大家好,我setDefaultPoint控制器返回地图作为JSON ...的code低于

 高清setDefaultPoint = {
    如果(springSecurityService.isLoggedIn()){
        高清officeId = params.officeId
        高清officeRoleInstance = OfficeRole.get(officeId)
        DEF公司= officeRoleInstance.company
        高清defaultPoint = officeRoleInstance.distanceChart.officePoint
        高清地图= [defaultPoint:defaultPoint]
        地图<< [公司:公司]
        高清JSON =映射为JSON
        呈现JSON
    }
 

Ajax调用它发送请求到该控制器是

 函数setDefaultPoint(){
        。VAR officeId = $('#clientTrip_officeId)VAL();

        $阿贾克斯({
            网址:$ {createLink(控制器:'clientTrip,动作:setDefaultPoint')},
            数据类型:JSON,
            数据:({officeId:officeId}),
            成功:功能(数据){

                //这里我想获得的违约点和id和公司名称的标识和名称...
        的console.log('defaultpoint的id是---+ ????)
                    的console.log(默认点名称是---+ ????)
                    的console.log(公司编号为-----------+ ......)
            }

        });
    }
 

这是JSON我传递了两个不同的对象。那么如何让这些对象的propirties ......无论defaultPoint目标和公司目标已经称为字段名和ANME

该响应的样子

  {defaultPoint:{类:com.springpeople.steer.trips.Point,ID:3,名: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}}
 
抛弃jQuery,拥抱原生JavaScript

解决方案

由于返回的数据类型指定为 JSON 数据参数传递到分配给成功将是从返回的JSON字符串解析JavaScript对象的功能,只要该字符串是有效的JSON。在数据在Ajax调用传递不需要周围的括号。这应该工作

 函数setDefaultPoint(){
    。VAR officeId = $('#clientTrip_officeId)VAL();

    $阿贾克斯({
        网址:$ {createLink(控制器:'clientTrip,动作:setDefaultPoint')},
        数据类型:JSON,
        数据:{officeId:officeId},
        成功:功能(数据){
            的console.log(data.defaultPoint.id,data.defaultPoint.name);
            的console.log(data.company.id,data.company.name);
        }

    });
}
 

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

解决方案

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

    });
}