JSON Data probably not formatted correctly
I have the following script which does not work 100%, it returns about 20 undefined and somewhere in between those undefined, it will return the full_name:
function get_staff_details(phrase) {
$.ajax({
url: 'get_staff_details.aspx?rand=' + Math.random(),
type: 'POST',
dataType: 'json',
data: { strPhrase:phrase },
error: function(xhr, status, error) {
console.log(status);
console.log(xhr.responseText);
},
success: function(results) {
var itemList = results;
var buildHTML = [];
for (var i = 0; i < itemList.length; i++) {
buildHTML.push('<div class="container"><a href="#" class="title" target="_blank">' + itemList[i].full_name + '</a></div>');
}
$('.portlet-content').empty().append(buildHTML.join('<hr />'))
}
});
}
I think it has something to do with the json data, which is probably not formatted correctly to loop through it???
开发者_运维百科[
{
"image": "http://intranet/images/jb.jpg"
},
{
"position": "Marketing Manager"
},
{
"cms_initials": "JB"
},
{
"department_name": "Marketing"
},
{
"secretary": ""
},
{
"fee_earner": ""
},
{
"mon_from": "08:30"
},
{
"mon_to": "17:00"
},
{
"tue_from": "08:30"
},
{
"tue_to": "17:00"
},
{
"wed_from": "08:30"
},
{
"wed_to": "17:00"
},
{
"thu_from": "08:30"
},
{
"thu_to": "17:00"
},
{
"fri_from": "08:30"
},
{
"fri_to": "17:00"
},
{
"full_name": "Jo Bloggs"
},
{
"extension": "0000"
},
{
"direct_line_number": "0000000000"
},
{
"blackberry_number": ""
}
]
As hvgotcodes said, what you have there is an array with a bunch of individual entries, each of which is an object with just one property (and each of which has a different property).
You may have wanted this:
[
{
"image": "http://intranet/images/jb.jpg",
"position": "Marketing Manager",
"cms_initials": "JB",
"department_name": "Marketing",
"secretary": "",
"fee_earner": "",
"mon_from": "08:30",
"mon_to": "17:00",
"tue_from": "08:30",
"tue_to": "17:00",
"wed_from": "08:30",
"wed_to": "17:00",
"thu_from": "08:30",
"thu_to": "17:00",
"fri_from": "08:30",
"fri_to": "17:00",
"full_name": "Jo Bloggs",
"extension": "0000",
"direct_line_number": "0000000000",
"blackberry_number": ""
}
]
...which is an array with only one entry, an object with a whole list of properties. (Presumably you'd have more than one object in total, but there's only one above.) If so, you'll need to change the code generating the JSON.
Its not a formatting problem. You have an array. Each element in the array is an object literal. Each object literal has a property -- each one has a different property. 'full_name' is a property on only one object literal -- 4 up from the bottom.
In other words, not every element in the array has a full_name
property. If they all did, you would not get undefined.
That JSON is a single array of objects. Only one of those objects has a "full_name" property.
If you want just that value, you could change your loop:
for (var i = 0; i < itemList.length; i++) {
if (itemList[i].hasOwnProperty('full_name'))
buildHTML.push('<div class="container"><a href="#" class="title" target="_blank">' + itemList[i].full_name + '</a></div>');
}
精彩评论