weixin_33744854 2013-08-31 16:00 采纳率: 0%
浏览 36

Ajax未知警报错误

i wasa following some ajax tutorials till the completion of first code but i am getting error and since i am not really very familiar with ajax i am not getting the error, i understand the things but this code was working fine in his tutorial but when i run it it goes to the last else and executs alert("Error in the end yarr")...... can someone help me please,thank u

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>'
?> 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
    • ¥15 js调用html页面需要隐藏某个按钮
    • ¥15 ads仿真结果在圆图上是怎么读数的
    • ¥20 Cotex M3的调试和程序执行方式是什么样的?
    • ¥20 java项目连接sqlserver时报ssl相关错误
    • ¥15 一道python难题3
    • ¥15 牛顿斯科特系数表表示
    • ¥15 arduino 步进电机
    • ¥20 程序进入HardFault_Handler
    • ¥15 oracle集群安装出bug