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;
});
1
  • you need to show what is inside data.. Also you will need to parse data into a javascript object Commented May 8, 2013 at 0:17

2 Answers 2

2

Try specifying the dataType as 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": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.

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

Comments

0

I think you just need to do:

data[0].officeId

Comments

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.