【问题标题】:jQuery AJAX Request ErrorjQuery AJAX 请求错误
【发布时间】:2012-01-30 09:54:15
【问题描述】:

我曾经使用 jQuery 完成了一些 AJAX 请求,但由于某种原因,我无法让这个请求工作。

我的代码所做的是询问用户他们的位置,使用 Google Maps API 对其进行地理编码,并将最近的 5 个商店返回给用户。

如果我使用手动输入的 URL 参数在浏览器中访问由 ajax 请求调用的页面,那么它可以正常工作,例如www.domain.com/storelocator/geocode.php?address=xxxxxx

index.php

<form id="storeform">
    <input type="text" id="address" size="50"/>
    <span><input id="submit" type="image" src="../images/btnsmall.jpg" value="Search" /></span>
</form>


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maps.google.com/maps/api/js?libraries=geometry&sensor=false" ></script>
<script type="text/javascript">
$(document).ready(function() {

    $('#storeform').submit(function() {
        var address = $('#address').val();
        var params = $('#storeform').serialize();

        if (address == '') {
        alert('Please enter a location');
        }

        $.ajax({
            url: "geocode.php",
            data: params,
            dataType: 'html',
            type: 'GET',
            success: function(response){
                 $('#stores').html(response);        
            }
        }); 
    });
});
</script>

geocode.php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$db_conn = mysql_connect('xxxxxx', 'xxxxxx', 'xxxxx') or die('error');
mysql_select_db('xxxx', $db_conn) or die('error');

$address = $_GET['address'];
$earth_radius = 3959;
$ch = curl_init('http://maps.google.com/maps/api/geocode/json?address='.$address.'&sensor=false');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geocode = curl_exec($ch);
curl_close($ch);

$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$lng = $output->results[0]->geometry->location->lng;

$sql = "SELECT *, ( $earth_radius * acos( cos( radians($lat) ) * cos( radians( latitude ) ) * cos( radians( longitude ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( latitude ) ) ) ) AS distance FROM stores HAVING distance < 25 ORDER BY distance LIMIT 5";
$result = mysql_query($sql);
$stores = '';

if (mysql_num_rows($result) > 0){
    while($row = mysql_fetch_array($result))
    {
        $stores .= $row['town'].' - '.$row['name'];
        $stores .='<br />';
    }
}
else {
    $stores = 'No results!';
}

echo $stores;

在 Firebug 中很难看到错误,因为它来去匆匆,但不知何故它看起来像是 geocode.php 中的内部服务器错误。

另外,当我提交表单时,我的 url 中会出现奇怪的 x 和 y 参数,例如

http://www.domain.com/store-locator/index.php?x=13&amp;y=11 我不知道为什么。

任何帮助将不胜感激。

【问题讨论】:

  • 在您的萤火虫上单击“Persist”,如果该页面上存在错误,您应该会看到错误。由于您使用输入类型“image”作为提交,x & y 是点击坐标你在 url 中看到
  • 点击 Firebug 控制台上的“Persist”按钮,URL 更改时数据不会被擦除。
  • 因为您使用的是图像提交,所以浏览器会自动为单击图像的 x 和 y 坐标附加 GET 参数。因此,在您的示例中,在 (13,11) 处单击了图像按钮
  • 感谢有关坚持的提示。这是说我的geocode.php 文件有错误,但它指示的错误位置是jquery.min 文件,这显然是错误的! 编辑谢谢亚当,我想我可能很久以前就读到过,但完全忘记了,谢谢你清理它!
  • 在执行 curl_init(....) 之前使用 urlencode($address)

标签: php jquery google-maps-api-3


【解决方案1】:

如果你像这样使用 .submit() ,它肯定会刷新页面(很可能在请求仍在运行时)。您应该“返回 false”以防止这种默认行为。

$(document).ready(function() {

$('#storeform').submit(function() {
    var address = $('#address').val();
    var params = $('#storeform').serialize();

    if (address == '') {
    alert('Please enter a location');
    }

    $.ajax({
        url: "geocode.php",
        data: params,
        dataType: 'html',
        type: 'GET',
        success: function(response){
             $('#stores').html(response);        
        }
    }); 

    return false;
});
});

【讨论】:

  • 谢谢,return false 让它进入下一阶段,它仍然无法正常工作,但如果我无法弄清楚,我会为新问题提出另一个问题。
猜你喜欢
  • 1970-01-01
  • 2010-11-05
  • 2012-08-17
  • 1970-01-01
  • 2015-09-05
  • 2011-07-11
  • 1970-01-01
  • 2020-04-23
  • 2014-07-06
相关资源
最近更新 更多