Ajax,无法加载xml数据

时间:2015-06-04 11:55:44

标签: javascript php jquery ajax xml

虽然我试图通过ajax加载我的xml数据但它不起作用.. 我看不到数据显示..

var xmlHttp = createXMLHttpReq();    
function createXMLHttpReq() {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        try {
            xmlHttp = new ActiveXObject("Microsogt.XMLHTTP");
        } catch (e) {
            alert("Not supported for your browser");
        }
    }
    return xmlHttp;
}

function exam(id) {
    if (xmlHttp.readyState == 0 || xmlHttp.readyState == 4) {
        xmlHttp.open("GET", "examajax.php?id=" + id, true);
        xmlHttp.onreadystatechange = handleResponse;
        xmlHttp.send();
    } else {
        setTimeOut(exam(), 1000);
    }
}

function handleResponse() {
    if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
        data = xmlhttp.responseXML.documentElement.firstChild.data;
        document.getElementById("mainform").innerHTML = data;
    }
}

examajax.php文件:

<?php
    header("Content-Type : text/xml");
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
    echo '<response>';
        $id = isset($_GET['id']) ? intval($_GET['id']) : 0;
        $p = isset($_GET['s_p']) ? intval($_GET['s_p']) : 1;
        $loadAll = mysql_query("SELECT * FROM `questions` WHERE `testid` = $id");
        $ofp = mysql_num_rows($loadAll);
        $selected = (isset($_GET['sel']) && is_numeric($_GET['sel']) && $_GET['sel'] <= $ofp && $_GET['sel'] >= 1) ? $_SESSION['sel'.$p] = intval($_GET['sel']) : null;
        $loadQuestion = mysql_query("SELECT * FROM `questions` WHERE `testid` = $id && `pos` = $p ORDER BY -`id` DESC LIMIT 1");
        while ($question = mysql_fetch_array($loadQuestion)) {
            ?>
            שאלה <?php echo $p; ?>/<?php echo $ofp; ?>
            <?php echo $question['name']; ?>
            <input type="radio" onclick="select(1,<?php echo $p; ?>)" value="<?php echo $question['op1']; ?>" />
            <input type="radio" onclick="select(2,<?php echo $p; ?>)" value="<?php echo $question['op2']; ?>" />
            <input type="radio" onclick="select(3,<?php echo $p; ?>)" value="<?php echo $question['op3']; ?>" />
            <input type="radio" onclick="select(4,<?php echo $p; ?>)" value="<?php echo $question['op4']; ?>" />
            <input type="radio" onclick="select(5,<?php echo $p; ?>)" value="<?php echo $question['op5']; ?>" />
            <?php
        }
    echo '</response';
?>

如果我将innerHTML更改为某些字符串而不是数据,则会显示字符串..但由于某种原因,它不会加载xml数据..请帮助吗?

0 个答案:

没有答案