XHR未击中3 - '正在加载'

时间:2016-05-25 20:57:22

标签: javascript php ajax xml xslt

我的XHR对象没有达到'loading'状态,3。我无法弄清楚问题是什么,因为我找不到任何语法问题。它似乎是某种代码/语法错误,因为我在控制台中遇到的错误是“无法加载资源:服务器响应状态为500(内部服务器错误)”

无论如何,这就是我所拥有的。上面的错误在'xhr.send(requestBody);'行上触发在'lecturers.js'。

更新

“lecturers.js”的原始代码是:

function getResults(){
 xhr.open("GET", "lecturers.php?id=" + Number(new Date), true);
 xhr.onreadystatechange = getData;
 xhr.send(null);    
}

我想要做的是更改上面的代码以使用POST协议。

lecturers.htm

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
        <title>Course Lecturers</title>
        <script type="text/javascript" src="lecturers.js"></script>
    </head>
    <body>
        <form>
            <input type = "button" id="butt1" value = "Press for Results" /><br />
        </form>
        <script type="text/javascript">

            var btnResults = document.getElementById("butt1");
            btnResults.onclick = getResults();
        </script>
        <span id="results" /></span>
    </body>
</html>

lecturers.js

var xhr = false;

if(window.XMLHttpRequest){
    xhr = new XMLHttpRequest();
} else if(window.ActiveXObject){
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}


function getResults(){
    if(xhr){
        var dataSource = "lecturers.php";
        var requestBody = "lecturers.php?id=" + Number(new Date);
        xhr.open("POST", dataSource, true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = getData;
        xhr.send(requestBody);
    }
}
function getData ()
{
    alert(xhr.readyState);

    if ((xhr.readyState == 4) &&(xhr.status == 200))
    {    
        var object = document.getElementById("results");
        object.innerHTML = xhr.responseText;
    }
}

lecturers.xml

<?xml version="1.0" encoding="iso-8859-1"?>
<Courses>
    <Course>
        <title>Web Development</title>
        <lecturer>Jian Yu</lecturer>
        <moderator>Gary Snail</moderator>
        <points>25</points>
        <group>SCCRL</group>
        <Faculty>DCT</Faculty>
    </Course>

    <Course>
        <title>Software Development in Java</title>
        <lecturer>SpongeBob SquarePants</lecturer>
        <moderator>Patrick Star</moderator>
        <points>25</points>
        <group>SCCRL</group>
        <Faculty>DCT</Faculty>
    </Course>

    <Course>
        <title>Financial Accounting</title>
        <lecturer>Squidward Tentacles</lecturer>
        <moderator>Mr Crab</moderator>
        <points>10</points>
        <group>KEDRI</group>
        <Faculty>DCT</Faculty>

        <Course>
            <title>IS Project</title>
            <lecturer>Plankton</lecturer>
            <moderator>Sandy Cheeks</moderator>
            <points>30</points>
            <group>IS</group>
            <Faculty>DCT</Faculty>
        </Course>
    </Course>
</Courses>

lecturers.xsl

<?xml version="1.0"?><!-- DWXMLSource="results.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <HTML>
            <HEAD>
                <TITLE> Courses</TITLE>
            </HEAD>
            <BODY>
                Courses with More than 15 Credit Points : <BR/>
                <table border="1">
                    <tr>
                        <th>Title</th>
                        <th>Group</th>
                        <th>Faculty</th>
                    </tr>

                    <xsl:for-each select="Course">
                        <tr>
                            <td><xsl:value-of select="title"></xsl:value-of></td>
                            <td><xsl:value-of select="group"></xsl:value-of></td>
                            <td><xsl:value-of select="Faculty"></xsl:value-of></td>
                        </tr>
                    </xsl:for-each>
                </table>


                ----------
                <BR/>
                Number of Courses Offered by CSSE :

            </BODY>
        </HTML>
    </xsl:template>
</xsl:stylesheet>

lecturers.php

<?php
    $xmlDoc = new DomDocument;
    $xmlDoc->load("lecturers.xml");
    $xslDoc = new DomDocument;
    $xslDoc->load("lecturers.xsl");
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xslDoc);
    echo $proc->transformToXML($xmlDoc);
?> 

0 个答案:

没有答案