Xhr要求不要打3 - '加载' [英] Xhr request not hitting 3 - 'loading'
问题描述
我正在尝试使用带有XMLHttpRequest的POST协议将数据发送到服务器,但是xhr对象没有点击'3'。我知道这是因为代码错误,因为我一直收到500错误,但我不知道如何更改代码以使用POST协议。我正在关注一个例子,但它不能用于此代码。
这个:
< pre lang =Javascript> var dataSource = 讲师.PHP跨度>;
var requestBody = test跨度>;
alert(requestBody);
xhr.open( POST,dataSource,真跨度>);
xhr.setRequestHeader( Content-Type, application / x-www-form-urlencoded);
xhr.onreadystatechange = getData;
xhr.send(requestBody);
原来是这样的:
xhr.open( GET, lecturers.php?id = + Number ( new Date ), true );
xhr.onreadystatechange = getData;
xhr.send( null );
讲师.htm
< html xmlns = http://www.w3.org/1999/xhtml\">
= text / html; charset = iso-8859- 1 / >
< head >
< ; meta http-equiv = 内容类型 < span class =code-attribute> content
< title > 课程讲师< / title >
< script type = text / javascript src = lecturers.js > < / script >
< / head >
< body >
< 表格 >
< 输入 type = button id = butt1 value = 按结果s / > < br / >
< / form >
< < span class =code-leadattribute> script type = text / javascript >
var btnResults = document .getElementById( BUTT1\" 跨度>);
btnResults.onclick = getResults;
< / script >
< span id = results / > < / span > ;
< / body >
< / html >
讲师.js < br $> b $ b
< p re lang =Javascript> var xhr = false;
if(window.XMLHttpRequest){
xhr = new XMLHttpRequest();
} else if(window.ActiveXObject){
xhr = new ActiveXObject(Microsoft.XMLHTTP);
}
函数getResults(){
if(xhr){
var dataSource =lecturers.php;
var requestBody =test;
alert(requestBody);
xhr.open(POST,dataSource,true);
xhr.setRequestHeader(Content-Type,application / x-www-form-urlencoded);
xhr.onreadystatechange = getData;
xhr.send(requestBody);
}
}
函数getData()
{
alert(xhr.readyState);
if((xhr.readyState == 4)&&(xhr.status == 200))
{
var object = document.getElementById ( 结果);
object.innerHTML = xhr.responseText;
}
}
lecturers.xml
< pre lang =xml><?xml version =1.0encoding =iso- 8859-1\" >?;
<课程>
<课程>
< title>网站开发< / title>
<讲师> Jian Yu< /讲师>
< moderator> Gary Snail< / moderator>
< points> 25< / points>
< group> SCCRL< / group>
< Faculty> DCT< / Faculty>
< /课程>
<课程>
< title> Java中的软件开发< / title>
<讲师> SpongeBob SquarePants< /讲师>
< moderator> Patrick Star< / moderator>
< points> 25< / points>
< group> SCCRL< / group>
< Faculty> DCT< / Faculty>
< /课程>
<课程>
< title>财务会计< / title>
<讲师> Squidward Tentacles< /讲师>
< moderator> Mr Crab< / moderator>
< points> 10< / points>
< group> KEDRI< / group>
< Faculty> DCT< / Faculty>
<课程>
< title> IS Project< / title>
<讲师> Plankton< /讲师>
< moderator> Sandy Cheeks< / moderator>
< points> 30< / points>
< group> IS< / group>
< Faculty> DCT< / Faculty>
< /课程>
< /课程>
< /课程>
讲师xsl < br $>
< pre lang =HTML><?xml version =1.0?><! - DWXMLSource = results.xml - >
< xsl:stylesheet version =1.0xmlns:xsl =http://www.w3.org/1999/XSL/Transform>
< xsl:output method =html/>
< xsl:template match =/>
< HTML>
< HEAD>
< TITLE>课程和LT; / TITLE>
< / HEAD>
< BODY>
超过15个学分的课程:< BR />
< table border =1>
< tr>
< th>标题< / th>
< th> Group< / th>
< th> Faculty< / th>
< / tr>
< xsl:for-each select =Courses>
< 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 />
CSSE提供的课程数量:
< / BODY>
< / HTML>
< / xsl:template>
< / xsl:stylesheet>
讲师。 php
<?php
$ xmlDoc = new DomDocument;
$ xmlDoc-> load( lecturers.xml);
$ xslDoc = new DomDocument;
$ xslDoc-> load( lecturers.xsl);
$ proc = new XSLTProcessor;
$ proc-> importStyleSheet($ xslDoc);
echo test ;
echo $ proc-> transformToXML($ xmlDoc);
?>
我有什么尝试过:
我试过改变一切,改变变量的值等。 xmlDoc
= new DomDocument;
xmlDoc-> load( lecturers.xml);
xslDoc
= new DomDocument;
Hi, I am trying to send data to a server using POST protocol with XMLHttpRequest, but the xhr object isn't hitting '3'. I know this is because of a code error because I keep getting a 500 error, but I don't know how to change the code to use POST protocol. I'm following an example, but it's not working for this code.
This:
var dataSource = "lecturers.php";
var requestBody = "test";
alert(requestBody);
xhr.open("POST", dataSource, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = getData;
xhr.send(requestBody);
Was originally this:
xhr.open("GET", "lecturers.php?id=" + Number(new Date), true);
xhr.onreadystatechange = getData;
xhr.send(null);
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
<pre lang="Javascript">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 = "test"; alert(requestBody); 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
<pre lang="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
<pre lang="HTML"><?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="Courses"> <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 "test";
echo $proc->transformToXML($xmlDoc);
?>
What I have tried:
I have tried changing things around, changing the values of variables etc.
xmlDoc = new DomDocument;
xmlDoc->load("lecturers.xml");
xslDoc = new DomDocument;
这篇关于Xhr要求不要打3 - '加载'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!