开发者

AJAX error: response is current page's html

开发者 https://www.devze.com 2023-04-11 13:05 出处:网络
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

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.

AJAX error: response is current page's html

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" valu开发者_如何学Goe="'.$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.


@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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号