我是在关注一些ajax教程直到完成第一个代码但我得到错误,因为我不是非常熟悉ajax我没有得到错误,我理解的东西,但这个代码在他的教程中工作正常但当我运行它时,它会转到最后一个和executs警报(“yarr中的错误”)......有人可以帮助我,谢谢你
dynamic.html
<!doctype html>
<html lang="en">
<head>
<script type="text/javascript" src="practice.js"></script>
</head>
<body onload="process()">
<h2>Ajax!! It Begins<h2>
Enter a muslim name :
<input type="text" id="mname"/>
<div id="show"></div>
</body>
</html>
的 practice.js 的
var xmlHttp = createXmlHttpRequestObject(); //xmlHttp --> a main object in Ajax
function createXmlHttpRequestObject(){
var xmlHttp;
if(window.ActiveXObject){ //when user not using IE
try{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(e){
xmlHttp = false;
}
}
else{
try{
xmlHttp = new XMLHttpRequest();
}catch(e){
xmlHttp = false;
}
}
if(!xmlHttp){
alert("Errorr yrrrrrr!!");
}
else{
return xmlHttp; //because in the first line , it is equal to the function so needs to return
}
}
function process(){ //takes that Communication object(xmlHttp) and sends request to the server
if(xmlHttp.readyState==0 || xmlHttp.readyState==4){
name = encodeURIComponent(document.getElementById('mname').value);
xmlHttp.open("GET", "muslims.php?name="+name, true); //if it should be handled asyncrounsly
xmlHttp.onreadystatechange = handleServerResponse; //this handles the server response after you send request to server
xmlHttp.send(null);
}
else{
setTimeout('process()',1000);
}
}
function handleServerResponse(){ //sends back xml code in php file
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){ //200 means communication went normal
document.write("Enters 44");
xmlResponse = xmlHttp.responseXML; //basically xmlResponse is now your xml from php file
xmlDocumentElement = xmlResponse.DocumentElement;
message = xmlDocumentElement.FirstChild.data; //this message will be equal to one of the echos from php file
document.getElementById('show').innerHtml = '<span style="color:red">'+ message + '</span>';
setTimeout('process()',1000);
}
else{
alert("Error in the end yarrrrrrr!!!");
}
}
}
的 muslims.php 的
<?php
header('Content-type: text/xml');
echo '<?xml version="1.0" encoding:"UTF-8" standalone:"yes" ?>'
echo '<response>'
$name = $_GET['name'];
$namearray = array('Muhammad', 'Ali' , 'Fatima', 'Hassan', 'Hussain');
if(in_array($name,$namearray)){
echo 'Yay!!!! It is a muslim name , whatever';
}
else if($name==''){
echo 'You have to enter some name in the field you dumbass';
}
else{
echo 'Either your name is not muslim or we dont have this name, please dobara hath pair marain';
}
echo '</response>'
?>