Click here to Skip to main content
15,884,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends

I am trying to get the data from database using $.post() it returns me data
I am again using $.post() inside the fist one
and i am trying to push that data in array inside second $.post()
array length is showing inside second $.post() but outside it it is showing length 0

Below is my code
JavaScript
$.post('/Common/getInfo/', null, function (data) {
    var desc = [];
    var ultlng =[];
    var count = 0;

    for (var i = 0; i < data.length; i++) {
        desc.push(data[i].Description);
        $.post('/Common/getLatlngInfo/', { id: data[i].ID }, function (data1) {

            for (var j = 0; j < data1.length; j++) {
                if (j == 0) {
                    ultlng.push(new google.maps.LatLng(data1[j].Latitude, data1[j].Longitude));
                }

                if (data1.length > 1) {
                    if (j == data1.length - 1) {
                        var flightPlanCoordinates = [
                                    new google.maps.LatLng(data1[0].Latitude, data1[0].Longitude),
                                    new google.maps.LatLng(data1[1].Latitude, data1[1].Longitude),
                                    new google.maps.LatLng(data1[2].Latitude, data1[2].Longitude),
                                    new google.maps.LatLng(data1[3].Latitude, data1[3].Longitude),
                                    new google.maps.LatLng(data1[4].Latitude, data1[4].Longitude)
                        ];
                        var flightPath = new google.maps.Polyline({
                            path: flightPlanCoordinates,
                            geodesic: true,
                            strokeColor: '#007ACC',
                            strokeOpacity: 1.0,
                            strokeWeight: 3
                        });

                        flightPath.setMap(map);
                    }
                }
                else {
                    ultlng.push(new google.maps.LatLng(data1[j].Latitude, data1[j].Longitude));
                }
            }
        });
    }

    for (var i = 0; i < desc.length; i++) {
        marker = new google.maps.Marker({
            map: map,
            position: ultlng[i]
        });

        (function (i, marker) {
            google.maps.event.addListener(marker, 'click', function () {

                if (!infowindow) {
                    infowindow = new google.maps.InfoWindow();
                }

                infowindow.setContent("<html><body><div style="min-height:50px; max-width: 200px;">" + data[i].Description + "</div></body></html>");
                infowindow.open(map, marker);
            });

        })(i, marker);
    }
});



getting ultlng[] array length as 0 it should be 5
as 5 five elements are pushed into it

but outside $post() it is showing o lenght

Please get me solution for this,

Thanks in advance
Posted
Updated 3-Dec-14 22:27pm
v3
Comments
Kornfeld Eliyahu Peter 4-Dec-14 3:18am    
Not clear. Which length should be 0 or not zero. You have data, data1 and desc that you check for length, where is the problem?
Did you used debugger to check what actually your code does?
pRaNaY_.net 4-Dec-14 4:28am    
ultlng[] length should be 5 but it is showing me 0

1 solution

In JavaScript variable has scope[^] of global or local (without var it is global, with var it is local)...
ultlng is a variable scoped to the $post method and do not exists outside it...
You may change your code like this:
JavaScript
var desc = [];
var ultlng =[];
var count = 0;

$.post('/Common/getInfo/', null, function (data) {
  // do whatever you need
});

// use results...
 
Share this answer
 

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