我有一个使用 JSON 收集数据的 jQuery 商店定位器文件,但每次我尝试加载页面时它都会出错。
未捕获的类型错误:无法读取未定义的属性“0”
显示代码的行在下面突出显示
if(settings.jsonData == true){ dataType = "json"; }
else{ dataType = "xml"; }
$.ajax({
type: "GET",
url: settings.dataLocation,
dataType: dataType,
success: function(data) {
//After the store locations file has been read successfully
var i = 0;
if(settings.jsonData == true)
{
//Process JSON
$.each(data, function() {
console.log(data);
var name = this.locname;
var lat = this.lat;
var lng = this.lng;
var address = this.address;
var address2 = this.address2;
var city = this.city;
var postcode = this.postcode;
var phone = this.phone;
var web = this.web;
web = web.replace("http://","");
var hours1 = this.hours1;
var hours2 = this.hours2;
var hours3 = this.hours3;
var distance = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,lat,lng, GeoCodeCalc.EarthRadiusInMiles);
//Create the array
locationset[i] = new Array (distance, name, lat, lng, address, address2, city, postcode, phone, web, hours1, hours2, hours3);
i++;
});
}
else
{
//Process XML
$(data).find('marker').each(function(){
//Take the lat lng from the user, geocoded above
var name = $(this).attr('name');
var lat = $(this).attr('lat');
var lng = $(this).attr('lng');
var address = $(this).attr('address');
var address2 = $(this).attr('address2');
var city = $(this).attr('city');
var postcode = $(this).attr('postcode');
postcode = postcode.replace(" ","");
var phone = $(this).attr('phone');
var web = $(this).attr('web');
web = web.replace("http://","");
var hours1 = $(this).attr('hours1');
var hours2 = $(this).attr('hours2');
var hours3 = $(this).attr('hours3');
var distance = GeoCodeCalc.CalcDistance(orig_lat,orig_lng,lat,lng, GeoCodeCalc.EarthRadiusInMiles);
//Create the array
locationset[i] = new Array (distance, name, lat, lng, address, address2, city, postcode, phone, web, hours1, hours2, hours3);
i++;
});
}
//Sort the multi-dimensional array numerically
console.log(locationset);
locationset.sort(function(a, b) {
var x = a[0];
var y = b[0];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
//Check the closest marker
**********if(locationset[0][0] > settings.distanceAlert)***********
{
alert("Unfortunately, our closest location is more than " + settings.distanceAlert + " miles away.");
}
即使我尝试 console.log 位置集,它也是空白的
编辑
这是我的 json 代码,根本不认为标题是正确的
<?php
header('Content-Type: application/json');
$results = mysql_query("SELECT sl_store AS locname, sl_latitude AS lat, sl_longitude AS lng, sl_address AS address, sl_address2 AS address2, sl_city AS city, sl_zip AS postcode, sl_phone AS phone, sl_url AS web, sl_hours AS hours1 FROM `wp_store_locator`");
$rows = array();
while($r = mysql_fetch_assoc($results)) {
$rows[] = $r;
}
echo json_encode($rows);
?>