AJAX 错误:响应是当前页面的 html
我在这里遇到一个奇怪的问题,我正在开发的网站上的 AJAX 调用返回我当前所在页面的所有 html 标记,正如您在此处看到的那样。此函数应该仅从数据库中返回用户选择的州和城市的邮政编码。
我在此站点上使用 Paul Irish 的 HTML5 Boilerplate 和 Modernizr,以使跨浏览器兼容性开发变得更容易。尽管我之前使用过这些没有任何问题,但我开始认为我与 HTML5 Boilerplate 使用的脚本之一存在某种 javaScript 冲突。
这是 js:
function getCitiesFromState(state, select)
{
if (window.XMLHttpRequest)
{
x = new XMLHttpRequest();
}
else
{
x = new ActiveXObject("Microsoft.XMLHTTP");
}
var ran = Math.round((new Date()).getTime() / 1000),
terms = "state="+state+'&r='+ran, loader;
x.open("POST", "inc/ajax/cities-from-state.php", true);
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
x.onreadystatechange = function()
{
select == 'fcity'
? loader = 'fzip-loader'
: loader = 'tzip-loader';
$('#' + loader).show();
if (x.readyState==4 && x.status==200)
{
$('#' + select).html(x.responseText);
$('#' + loader).hide();
}
}
x.send(terms);
}
我确实知道 jQuery 具有广泛的 AJAX 功能,但在使用库或简写以使其更容易之前,我总是尝试了解代码背后的底层技术。
这是 php:
<?php
include 'db.class2.php';
$DB = new DB_MySql;
$DB->connect();
$state = $_POST['state'];
$q = $DB->query("
SELECT DISTINCT `city`, `zip_code`
FROM `usa_master`
WHERE `state` = '".addslashes($state)."'
GROUP BY `city`
ORDER BY `population` DESC LIMIT 0, 150"
);
$count = 0;
while($r = $DB->fetch_assoc($q))
{
$city[] = $r['city'];
$zips[$count] = $r['zip_code'];
if (4 == strlen($zips[$count]))
{
$zips[$count] = '0' . $zips[$count];
}
$count++;
}
array_multisort($city, $zips);
echo '<option value="" selected="selected">Select City</option>';
$size = sizeof($city);
for($x=0; $x<$size; $x++)
{
echo '<option class="city_list" value="'.$zips[$x].'">'.$city[$x].'</option>';
}
$DB->close();
?>
顺便说一句,我偶尔(也许每 10 次刷新一次)注意到我收到随机 jQuery 相关的 javaScript 错误,与我正在使用的 jQueryUI 日期选择器有关。我认为这里存在着我所缺少的相关性。
I'm having an odd issue here where an AJAX call on a site I'm developing is returning all the html markup of whatever page I'm currently on as you can see here. This function should merely return a zip code from a database for the state and city the user chooses.
I'm using Paul Irish's HTML5 Boilerplate and Modernizr on this site to make cross browser compatibility development easier. Although I've used these before with no issues, I'm starting to think that I'm having some kind of javaScript conflict with one of the scripts that HTML5 Boilerplate uses.
Here's the js:
function getCitiesFromState(state, select)
{
if (window.XMLHttpRequest)
{
x = new XMLHttpRequest();
}
else
{
x = new ActiveXObject("Microsoft.XMLHTTP");
}
var ran = Math.round((new Date()).getTime() / 1000),
terms = "state="+state+'&r='+ran, loader;
x.open("POST", "inc/ajax/cities-from-state.php", true);
x.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
x.onreadystatechange = function()
{
select == 'fcity'
? loader = 'fzip-loader'
: loader = 'tzip-loader';
$('#' + loader).show();
if (x.readyState==4 && x.status==200)
{
$('#' + select).html(x.responseText);
$('#' + loader).hide();
}
}
x.send(terms);
}
I do know jQuery has extensive AJAX functionality, but I always try to understand the underlying technology behind my code before using a library or shorthand to make it easier.
Here's the php:
<?php
include 'db.class2.php';
$DB = new DB_MySql;
$DB->connect();
$state = $_POST['state'];
$q = $DB->query("
SELECT DISTINCT `city`, `zip_code`
FROM `usa_master`
WHERE `state` = '".addslashes($state)."'
GROUP BY `city`
ORDER BY `population` DESC LIMIT 0, 150"
);
$count = 0;
while($r = $DB->fetch_assoc($q))
{
$city[] = $r['city'];
$zips[$count] = $r['zip_code'];
if (4 == strlen($zips[$count]))
{
$zips[$count] = '0' . $zips[$count];
}
$count++;
}
array_multisort($city, $zips);
echo '<option value="" selected="selected">Select City</option>';
$size = sizeof($city);
for($x=0; $x<$size; $x++)
{
echo '<option class="city_list" value="'.$zips[$x].'">'.$city[$x].'</option>';
}
$DB->close();
?>
On a side note, I'm noticing occasionally(maybe every 10th time I refresh) that I'm getting random jQuery related javaScript errors, having to do with a jQueryUI datepicker I'm using. I'm thinking there's a correlation here that I'm missing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Diodeus 就是这样。我在模板化这个新网站时首先编码了所有的javaScript,但我最近刚刚编码了导航结构和显然这个.htaccess规则(
RewriteRule ^(.*).php template.php?page=$1 [NC]
) 正在将我的 AJAX 请求重定向到我所服务的 404 页面。谢谢!@Diodeus that's what it is. I coded all my javaScript first while templating this new site but I just recently coded the navigation structure and apparently this .htaccess rule (
RewriteRule ^(.*).php template.php?page=$1 [NC]
) is redirecting my AJAX request to the 404 page I serve. Thanks!