In relation to this question: jQuery loop over JSON result from AJAX Success?
I've got the following which makes Firefox use up way too much cpu before returning too many items (new divs) with only some 'undefined' texts in it, although I can see (at a fiddle page I use) that the way I want to retrieve items (e.g. user.functionTitle) is correct. What is going wrong here?
var result = [{"id":7,
"loginID":"jdoe",
"userStatus":"ACTIVE",
"firstName":"John",
"middleName":"",
"prefix":"",
"lastName":"Doe",
"functionTitle":"Junior developer"},
{"id":8,
"loginID":"jadoe",
"userStatus":"ACTIVE",
"firstName":"Jane",
"middleName":"",
"prefix":"",
"lastName":"Doe",
"functionTitle":"Junior developer"}
];
And the following ajax statement which I try to debug with firebug, but after the line with "$.each(result, function(idx, user) {" it simply hangs untill Firefox tells me some javascript is taking too much time ...
$.ajax({
type : 'POST',
url : "../hrm/search",
contentType : 'application/x-www-form-urlencoded',
cache: false,
data: "name=" + $("#site-search").val(),
dataType : 'text',
success : function(result) {
if (result != null) {
$.each(result, function(idx, user) {
$('#found-users').append("<div class='option'>" +
"<div class='pass-wrapper'>" +
"</div>" +
"<div class='sr-content'>" + user.firstName + " " + user.prefix + " " + user.lastName + "</div>" +
"<div class='sr-content'>" + user.functionTitle + "</div>" +
+ "</div>");
});
$("#found-users").show;
} else {
$('#found-users').hide;
$('#found-users div').empty();
}
}
});
The code in the jsp page simply looks like this:
<div><input type="text" placeholder="search" id="site-search"></div>
<div id="found-users" class="search-results options">
</div>
First of all: If I put the data together with the following statement in a jsfiddle page it does exactly what it should do, it pops up with the function title twice. How can the same code execute way more than twice when I add div's to a page instead of pop up an alert? And second why does user.functionTitle suddenly return undefined?
$.each(result, function(idx, user) {
alert(user.functionTitle);
});