Note that there are some explanatory texts on larger screens.

plurals
  1. POJS / jQuery: Unable to access the properties of an object returned from an ajax call
    text
    copied!<p>I'm quite new to JS and jQuery.</p> <p>I have a problem accessing the properties of an object returned from the jQuery ajax method.</p> <p>As you can see in the code below I'm trying to use the properties of the object 'data' in the success call back function to create a table row, which I then try and add to a table.</p> <pre> $('#add_comp').submit(function(){ var startDate = $('#form_startDate').val(); var initPrize = $('#form_init_prize').val(); var active = $('#form_active').val(); var dataString = 'startDate=' + startDate + '&startPrize=' + initPrize + '&active=' + active; $.ajax({ type: 'POST', url: "/admin/competition/add-competition", data: dataString, success: function(data, textStatus, xhr){ console.log(data); console.log(data.startDate); console.log(data[startDate]); console.log(data['startDate']); var tr = '\n\ '+ data["startDate"] +'\n\ '+ data.active +'\n\ '+ data.currentPrize +'\n\ '; $('#competition_table').find('tr:last').after(tr); }, error: function(){ alert('There has been an error, please consult the application developer.'); } }); </pre> <p>The success function is a bit of a mess as I'm trying a number of different ways of accessing the properties of the data object.</p> <p>The first console.log(data) line returns the following in firebugs console:</p> <pre> {"competitionId":null,"startDate":"08\/02\/2010","endDate":null,"winner":null,"participantPool":"4c6729aa8c8fb","active":1,"startPrize":"350","currentPrize":"350"} </pre> <p>This confirms the data object is there and it has the correct properties.</p> <p>I assume I should be able to access the individual properties using 'data.propertyName' however all subsequent console.log() calls return 'undefined'.</p> <p>How to correctly access these properties and use them to build the table row?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload