0

我正在尝试根据用户在输入字段中提供的邮政编码获取城市名称。

这个输入和ajax函数:

<input type="text" name="postal_code" id="postal_code" 
onkeyup="
  $.get('<?=$config['url']?>/ajax/location/?code='+this.value, 
  function(data){ $('#location').html(data); });" 
  maxlength="5" />

这是 /ajax/location/?code= 文件:

 <?php
    $city = mysql_fetch_object(mysql_query("SELECT * FROM postal_codes WHERE postal_code = '" . intval($_GET['code']) . "'")); 
 ?>
 <div>
   <p>
      <input value="<?php echo $city->city_name; ?>" name="city"  />
   </p>
</div>

我使用 .ajaxError 来查看错误,但没有帮助。它仅警告 url 而没有有关错误的信息。

$(document).ajaxError(function(e, xhr, settings, exception) {
            alert('error in: ' + settings.url + ' \n'+'error:\n' + xhr.responseText );
            }); 

这就是 ajaxError 警报:

error in: http://mywebsitename.com/ajax/location/?code=11824 
error:

我尝试使用 $.get 获取 .txt 文件并且它有效。所以我 100% 确定 ajax 工作正常。

谢谢。

4

2 回答 2

0

/ajax/location/?code= 没有数据库连接

编辑:

试试这个:

$.get('<?=$config['url']?>/ajax/location/', {code:this.value}, function(data){ $('#location').html(data); });
于 2012-08-18T12:42:40.360 回答
0

解决了我的问题。

问题是:

<?=$config['url']?>

绝对网址。我删除了它并且它起作用了。

'/ajax/location/?code='+this.value

感谢大家试图帮助我!

于 2012-08-19T13:55:55.053 回答