1

I hesitated asking a this question in fear of getting the all feared down-vote, but after many searching and countless hours of typing, I must give in.

I'm just trying to get these values from a json object (ie 37 and exampleoffice):

{"officeId":37,"officeName":"exampleoffice"}

I have tried data[0], data.officeId, data[0][officeId], for loop, $.each(data, function(i, item){})...

I would appreciate if someone could help me and make this headache go away!

jquery:

$(document).on("click", "#addOffice", function() {
    var officeadd = $('#officeAddForm').serializeArray();
    console.log(officeadd);
    $.ajax({
        url:        'officeadd.php',
        type:       "POST",
        data:       officeadd,
        success: function(data) {
            console.log(data);
            $('#officecontrolgroup').append('<input type="radio" name="office" id="' + data.officeId + '" value="' + data.officeId + '"/><label for="' + data.officeId + '">' + data.officeName + '</label>').trigger('create');
            $('#officecontrolgroup').controlgroup("refresh");
            $( "#deliveryInstructions" ).trigger( "updatelayout" );
        }   
    });
    return false;
});
4

2 回答 2

2

尝试将 dataType 指定为 json。

$.ajax({
  url:      'officeadd.php',
  type:         "POST",
  data:         officeadd,
  success: function(data) {
                console.log(data);
                $('#officecontrolgroup').append('<input type="radio" name="office" id="' + data.officeId + '" value="' + data.officeId + '"/><label for="' + data.officeId + '">' + data.officeName + '</label>').trigger('create');
                $('#officecontrolgroup').controlgroup("refresh");
                $( "#deliveryInstructions" ).trigger( "updatelayout" );
           }   ,
   dataType: "json" 
});

“json”:将响应评估为 JSON 并返回一个 JavaScript 对象。JSON数据被严格解析;任何格式错误的 JSON 都会被拒绝并引发解析错误。

于 2013-05-08T00:19:10.733 回答
0

我认为你只需要这样做:

data[0].officeId
于 2013-05-08T00:18:36.730 回答