首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

AJAX 服务器回送数据失败 onreadystatechange没法触发回调函数

2013-02-17 
AJAX 服务器回送数据失败 onreadystatechange无法触发回调函数在一个文本标签中输入电子邮件,当onblur时触

AJAX 服务器回送数据失败 onreadystatechange无法触发回调函数
在一个文本标签中输入电子邮件,当onblur时触发验证事件,到数据库检验是否已经存在该用户,其中dispear方法即为onblur事件触发的方法。已经验证服务器已经收到本地发送的数据并且验证成功,但是echo不回去。
js代码如下:
var myXmlHttpRequest;
function getXmlHttpObject(){
   var xmlHttpRequest;
   try{
     xmlHttpRequest=new XMLHttpRequest();
     
   }
catch(e){
   try{
      xmlHttpRequest=new ActiveXObject("Msxml2.HMLHTTP");
      
   }
   catch(e){
      xmlHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
      
   }
 }
   return xmlHttpRequest;
 }




function isExist(email){

    myXmlHttpRequest=getXmlHttpObject();
    if(myXmlHttpRequest){
    alert("hello"+email);
    
    var url="/renren/register_legal_Process.php";
    var data="email="+email;
    myXmlHttpRequest.open("post",url,true);
    myXmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    // 指定回调函数
    
    
   myXmlHttpRequest。onreadystatechange=deal;
    myXmlHttpRequest.send(data);
    
    //alert(myXmlHttpRequest.readyState);
    }
}
function deal(){

alert('hello');
    if(myXmlHttpRequest.readyState==4){
    
    var isExist=myXmlHttpRequest.responseText;
    
    if(isExist=="exist"){
    $("focus1").style.display="none";
    $("wrong1").style.display="none";
    $("duihao").style.display="none";
    $('wrong5').style.display="block";
    }else if(isExist=="notexist"){
    $("focus1").style.display="none";
    $("wrong1").style.display="none";
    $("wrong5").style.display="none";
    $("duihao").style.display="block";    
    }
    
    
    } 
   
}
function $(id){
  return document.getElementById(id);
}  
function disappear(ref1,ref2,ref3,ref4){
    var app=$(ref1);
 var tmp1=$(ref2);
 var tmp2=$(ref3);
 var tmp3=$(ref4);
 var str=tmp1.value;
 var reg=/^([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\-|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/gi;

        if(!reg.test(str))
{
    tmp3.style.display="none";
 app.style.display="none";
 $("wrong5").style.display="none";
 tmp2.style.display="";
}
else
{   
/*
 * tmp2.style.display="none"; app.style.display="none";
 * tmp3.style.display="";
 */


//var email=$('email').value;
isExist(str);
                  }

 if(str=="")
 {
 app.style.display="none";
 tmp2.style.display="none";
 tmp3.style.display="none";
 } 
      
}
服务器端代码如下:
<?php
 header("Content-type:text/html;charset=utf-8;");
  header("Cache-Control:no-cache");
  require_once 'UserService.class.php';
    $email=$_POST['email'];
    //file_put_contents("log.txt",$email."\r\n",FILE_APPEND);
    $userService=new UserService();
    $isExist=$userService->isExist($email);
    if($isExist){
    file_put_contents("log.txt","exist"."\r\n",FILE_APPEND);
    echo "exist";  
    
    }else{
    file_put_contents("log.txt","notexist"."\r\n",FILE_APPEND);
    echo "notexist";
    
    }

?> xmlhttprequest ajax 服务器
[解决办法]
 myXmlHttpRequest.onreadystatechange=deal;  //全角点号改正了再说

另外,用 firebug调试看看
[解决办法]
做IT这行,一点小细节都不能忽略。

热点排行