Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php JSON-使用Javascript从文件收集数据时出错_Php_Javascript_Json_Uncaught Exception - Fatal编程技术网

Php JSON-使用Javascript从文件收集数据时出错

Php JSON-使用Javascript从文件收集数据时出错,php,javascript,json,uncaught-exception,Php,Javascript,Json,Uncaught Exception,我有一个jQuery存储定位器文件,它使用JSON收集数据,但每次我尝试加载页面时,它都会不断给出一个错误 未捕获类型错误:无法读取未定义的属性“0” 下面突出显示显示代码的行 if(settings.jsonData == true){ dataType = "json"; } else{ dataType = "xml"; } $.ajax({ type: "GET", url: settings.dataLocation, dataType: da

我有一个jQuery存储定位器文件,它使用JSON收集数据,但每次我尝试加载页面时,它都会不断给出一个错误

未捕获类型错误:无法读取未定义的属性“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.");
      }
if(settings.jsonData==true){dataType=“json”;}
else{dataType=“xml”;}
$.ajax({
键入:“获取”,
url:settings.dataLocation,
数据类型:数据类型,
成功:功能(数据){
//成功读取存储位置文件后
var i=0;
if(settings.jsonData==true)
{
//处理JSON
$.each(数据,函数(){
控制台日志(数据);
var name=this.locname;
var lat=this.lat;
var lng=此值为1.lng;
var address=this.address;
var address2=this.address2;
var city=this.city;
var 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距离=GeoCodeCalc.CalcDistance(原始纬度、原始lng、纬度、lng、GeoCodeCalc.EarthRadiusInMiles);
//创建数组
locationset[i]=新数组(距离、名称、纬度、液化天然气、地址、地址2、城市、邮政编码、电话、网络、小时1、小时2、小时3);
i++;
});
}
其他的
{
//处理XML
$(数据)。查找('marker')。每个(函数(){
//从用户处获取上述地理编码的lat lng
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');
邮政编码=邮政编码。替换(“,”);
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距离=GeoCodeCalc.CalcDistance(原始纬度、原始lng、纬度、lng、GeoCodeCalc.EarthRadiusInMiles);
//创建数组
locationset[i]=新数组(距离、名称、纬度、液化天然气、地址、地址2、城市、邮政编码、电话、网络、小时1、小时2、小时3);
i++;
});
}
//对多维数组进行数字排序
控制台日志(locationset);
locationset.sort(函数(a,b){
var x=a[0];
var y=b[0];
回报率((xy)?1:0);
});
//检查最近的标记
**********如果(locationset[0][0]>settings.distanceAlert)***********
{
警报(“不幸的是,我们最近的位置超过了”+settings.distanceAlert+“英里远”);
}
即使我尝试对locationset进行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);

?>


如果为空,则表示有什么东西在破坏(或没有构建)您的locationset数组。回溯您的代码以查看哪里/什么地方不起作用。您是否正确设置了服务器上的JSON标头?如果我们可以看到代码的这一面,我们可能会帮助您更轻松一些:-)
console.log
对象
locationset
settings
就在
//检查最近的标记
行代码,看看产生了什么。locationset为空,设置很好(只是文件开头定义的一些设置)。我在ajax请求之后放置了一个警报,效果很好,因此它位于两者之间。这可能是我的json代码吗?如果
console.log(数据);
是空的,那么问题是php代码。如果
数据
不是空的,请检查
数据
中的JSON并确保其有效。您不需要JSON头类型。您的JSON必须有效。