With jquery & ajax in a Symfony application, a button click is intended to retrieve a couple of properties of an entity. The first click of the button returns undefined; further clicks retrieve the expected values. How to get it the first time?
Script snippet:
$("#contact_household_button").click(function() {
var houseId = $("#contact_household").val();
if (houseId !== "") {
var url = "/app_dev.php/household/contact/" + houseId;
$.get(url, function(data) {
$("#household_store").data(data);
})
var household = $("#household_store");
var head = household.data('head');
alert(houseId + ": " + head);
}
});
Sample response provided by get():
{"id":8607,"head":"Some Person"}
First click yields
8607: undefined
2 - n clicks yield
8607 : Some Person