如何在html div中附加JSON数据? [英] How to append JSON data in the html div?
本文介绍了如何在html div中附加JSON数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<script type="text/javascript">
jQuery(document).ready(function()
{
var str = jQuery(location).attr('href');
var url_ProjectId = str.split("=")[1];
var url = "http://localhost/services/projects.php?method=browse_ar_projects&data=";
jQuery.ajax(
{
url: url,
dataType: 'json',
success: function (data, textStatus, jqxhr) {
CreateProperties(data);
},
error: function (jqxhr, textStatus, errorMessage) {
console.log(argument);
}
});
jQuery('#content').click(function ()
{
Android.NavigateToProjectOverview();
});
function CreateProperties(jsonData)
{
for (var i = 0; i < jsonData.data.length; i++)
{
var id=jsonData.data[i].project_id;
if(id==url_ProjectId)
{
var designString = '<div class="project_info">' + jsonData.data[i].project_name +'</div>';
jQuery('#content').append(designString);
}
else
{
console.log('error');
}
}
}
});
</script>
这是我的剧本。
我的JSON是:
This is my script.
My JSON is:
[{"status":"1","error":"0","data":[{"project_id":"1","project_name":"Hoysala ACE Phase II","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/2BHK.wt3","project_ar_type":"Apartment","project_ar_title":"Exterior","project_marker":"www.myfourwalls.in\/projects_data\/marker\/villa.wtc"},{"project_id":"2","project_name":"WILASA Aikya","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/table.wt3","project_ar_type":"Plots","project_ar_title":"Exterior","project_marker":"www.myfourwalls.in\/projects_data\/marker\/villa.wtc"},{"project_id":"3","project_name":"Bluejay Ardley","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/villa.wt3","project_ar_type":"Villa","project_ar_title":"Interior","project_marker":"www.myfourwalls.in\/projects_data\/marker\/villa.wtc"},{"project_id":"4","project_name":"Hoysala Habitat","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/seater_sofa.wt3","project_ar_type":"Apartment","project_ar_title":"360 Model","project_marker":"www.myfourwalls.in\/projects_data\/marker\/villa.wtc"},{"project_id":"5","project_name":"AVS","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/bedroom.wt3","project_ar_type":"Villa","project_ar_title":"Exterior","project_marker":"www.myfourwalls.in\/projects_data\/marker\/villa.wtc"},{"project_id":"6","project_name":"JSONS","project_ar_url":"www.myfourwalls.in\/projects_data\/villa\/bedroom.wt3","project_ar_type":"Villa","project_ar_title":"Exterior","project_marker":"www.myfourwalls.in\/projects_data\/marker\/villa.wtc"}]}]
我得到
I am getting
Uncaught TypeError: Cannot read property 'length' of undefined
此错误。
推荐答案
您的JSON数据是包含单个对象的数组;该对象包含一个名为data
的属性,这是您尝试访问的数组。
既然你'将整个数组传递给CreateProperties
函数,你实际试图访问名为data的属性数组本身就是code>。由于该属性不存在,它返回
undefined
,这就是当您尝试访问其长度$ c $时出现错误的原因c>属性。
假设JSON数组总是包含单个元素,请尝试将该元素传递给CreateProperties
而不是函数:
Your JSON data is an array containing a single object; that object contains a property calleddata
, which is the array you're trying to access.
Since you're passing the whole array to theCreateProperties
function, you're actually trying to access a property calleddata
on the array itself. Since that property doesn't exist, it returnsundefined
, which is why you get the error when you try to access itslength
property.
Assuming the JSON array always contains a single element, try passing that element to yourCreateProperties
function instead:
success: function (data, textStatus, jqxhr) {
CreateProperties(data[0]);
}
这篇关于如何在html div中附加JSON数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文