2013-08-18 52 views
0

我要開發一個互動地圖(谷歌地圖)。該座標通過從JSON解析AJAX,但我得到的錯誤錯誤Ajax和JSON數組

JSON格式

{"Level":[{"route":[{"lat":39.105251,"lgn":26.551727}, 
{"lat":39.105247125,"lgn":26.551774625}...],"balls": 
[{"lat":39.105239375,"lgn":26.551869875},{"lat":39.10524325,"lgn":26.55182225}]}, 
{"route":[{........},{.....}...],"balls":[{........},{.....}...]}]} 

Ajax請求

$.ajax({ 
    type: "GET", 
    url: "getcoordp?q=2", 
    dataType: "json", 
    cache: false, 
    contentType: "application/json", 
    success: function(data) { 
    $.each(data.Level, function(i,Level){ 
    $.each(data.route, function(index, route) { 
     alert(data.lat) 
     }); 
    }); 

    } 
    }); 

ERROR

Uncaught TypeError: Cannot read property 'length' of undefined 

有幫助嗎?

回答

0

更換下面

$.each(data.route, function(index, route)... 

隨着

$.each(Level.route, function(index, route) { 
    alert(route.lat) 
}); 

這將解決您的問題。

0

試試這個

for(i=0;i<data.Level.length;i++){ 
console.log(data.Level[i].route); 
}