【问题标题】:HTML not showing up on FirefoxHTML 未在 Firefox 上显示
【发布时间】:2011-06-25 18:46:15
【问题描述】:

我试图通过 AJAX 返回一个 PHP 页面,它总是在 Chrome 中加载。在 Firefox 中,它加载大约 5% 的时间,其他时候它什么都不加载,也没有 JS/PHP 错误。我只是在没有 CSS 的情况下直接回显 html。

这是我的 Ajax:

if(geo_position_js.init()){
            geo_position_js.getCurrentPosition(displayPosition,error_callback,{enableHighAccuracy:true,options:5000});
        }
        else{
            alert("Functionality not available");
        }

        function error_callback(p)
        {
            alert('error='+p.message);
        }       

    function displayPosition(loc) {
        var mylat = loc.coords.latitude;
        var mylong = loc.coords.longitude;
        $.ajax({
            type: "POST",
            url: "distancetest.php",
            data: "long="+mylong+"&lat="+mylat,
            success: function(html2){
                $('#locationinfo').html(html2);
                console.log(html);
           }
        });

    }

我的 PHP 基本上是这样做了几次:

$query = "SELECT * FROM tbl_geo WHERE cat_id=1";
    $result = mysql_query ($query) or die(mysql_error());
    echo "<h2>Restaurants</h2>";
    while ($row = mysql_fetch_array($result)){
        if($row['lat'] != ''){
            $distance = distance($_POST['lat'], $_POST['long'], $row['lat'], $row['lng'], "k");
            if($distance < 2000){
                $attractions[] = array('name' => $row['name'], 'address' => $row['address'], 'distance' => $distance);
            }
        }
    }
    $attractions = array_sort($attractions,'distance');
    $attractions = array_values($attractions);
    for ($i = 0; $i <= 10; $i++) {
        if(isset($attractions[$i]['distance'])){
            echo 'You are '.$attractions[$i]['distance'].'km away from '.$attractions[$i]['name'].' at '.$attractions[$i]['address'].'<br/>';
        }
    }

在某些浏览器中有效,但在其他浏览器中不显示。有什么想法吗?

更新:原来这是 Firefox 中的地理定位问题。它无法获得位置,但不会返回 error_callback 函数。现场示例在这里:http://adamzwakk.com/geolocate/

【问题讨论】:

  • 当它“什么都不返回”时,你确定AJAX请求真的出去了吗?
  • 使用 HTTPFox 或 Firebug 之类的东西来监控浏览器服务器通信。看看在这些暂停期间发生了什么。如果没有任何类型的诊断信息,几乎不可能弄清楚这一点。
  • 看起来 PHP 页面有时在 Firefox 中根本不会随机加载。使用 POST 和 GET 也没有区别。
  • 在其他几个浏览器中也会失败:veerasundar.com/blog/2010/02/… 这种缺乏支持会使其成为我合作的许多客户不支持的功能。
  • 我使用的是 Firefox 3.6 和 4.0 测试版,但两者仍然存在该错误。

标签: php jquery geolocation ip-geolocation


【解决方案1】:

谨慎使用 console.log()

在您的 javascript 代码中使用 console.log() 会在 firebug 被禁用或未安装时引发异常。 (95% 的时间?)
我写了一个小包装函数“dump(var)”dump.js,它检查是否启用了 firebug,因此在生产代码中使用它也是安全的。

PS: 我注意到:

 success: function(html2){
   $('#locationinfo').html(html2);
   console.log(html);
 }

html 变量是之前定义的还是您的意思是 html2

【讨论】:

    【解决方案2】:

    您确定您的浏览器支持您的操作吗?

    试试这个: http://html5demos.com/geo

    你也可以下载wireshark,查看你发送的和服务器返回的信息。

    将空白 php 页面(只是回显一个随机字符串)放在要生成 php 的同一目录中并查看它是否正常工作也是一个好习惯。在基于 Windows 的服务器上,PHP 可以设置为在特定目录中工作是一个常见问题,并且由于错误安装可能会在其他目录中失败。 每个浏览器处理错误和警告消息的方式都不同,所以如果它在 chrome 中运行,并不意味着它适用于其他所有浏览器。你试过 Safari 和 Opera 吗?

    【讨论】:

    • 您也可以下载wireshark,查看您发送的和服务器返回的信息。 - 萤火虫也显示它
    【解决方案3】:

    我打开了 FireBug,你点击了链接找到了我的地址。它返回大量链接说“未定义”。在 FireBug 中,它显示您的脚本正在返回:

    <?xml version="1.0" encoding="utf-8" ?>
      <geoData>
        <restaurants>
        </place>
        </restaurants>
        <accommodations>
        </accommodations>
        <things>
        </things>
      </geoData>
    

    我缩进它使它看起来更漂亮。您的应用程序似乎在 XML 中没有返回任何内容。您还关闭了一个未打开的标签!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 2021-06-12
      • 2023-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多