我正试图根据用户在输入字段中给出的邮政编码来获取城市名称。
这个输入,和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/?代码=文件:
<?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工作正常。
谢谢。
/ajax/location/?code=没有数据库连接
编辑:
试试这个:
$.get('<?=$config['url']?>/ajax/location/', {code:this.value}, function(data){ $('#location').html(data); });
解决了我的问题。
问题是:
<?=$config['url']?>
绝对url。我去掉了它,它起作用了。
'/ajax/location/?code='+this.value
感谢所有帮助我的人!