Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
<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>


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
this error.
Posted
Updated 25-Feb-15 1:26am
v3
Comments
Janardhanam Julapalli 25-Feb-15 7:06am    
This is my json data
[{"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"}]}]
Afzaal Ahmad Zeeshan 25-Feb-15 7:22am    
Append this JSON data in the question.
Sergey Alexandrovich Kryukov 25-Feb-15 7:07am    
Excuse me; append to what?
—SA
Janardhanam Julapalli 25-Feb-15 7:09am    
just inside a div
Afzaal Ahmad Zeeshan 25-Feb-15 7:22am    
Reply to their comment to get them notified of your answer.

1 solution

Your JSON data is an array containing a single object; that object contains a property called data, which is the array you're trying to access.

Since you're passing the whole array to the CreateProperties function, you're actually trying to access a property called data on the array itself. Since that property doesn't exist, it returns undefined, which is why you get the error when you try to access its length property.

Assuming the JSON array always contains a single element, try passing that element to your CreateProperties function instead:
JavaScript
success: function (data, textStatus, jqxhr) {
    CreateProperties(data[0]);
}
 
Share this answer
 
Comments
Janardhanam Julapalli 25-Feb-15 9:16am    
Spot on @Richard Deeming! I already fixed the issue. I have noticed this array in the debugger and it is giving jsonData.data as undefined. I have changed my web service call like this:
return json_encode($project_ar_response);
instead of
return json_encode(array($project_ar_response));.
Janardhanam Julapalli 25-Feb-15 9:18am    
Any way thanks for the reply. Nice to have experts like u people for help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900