0

我在我的 . 中编写了以下代码。通过单击提交按钮调用该函数home.phplogin()

 function login()
  {
    try{
      xmlHttp=new XMLHttpRequest();
    }catch(e){
      try{
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }catch(e2){
      try{
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
      }catch(e3){

      alert('Sorry ! AJAX not supported');
    }
    }
    }
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4&&xmlHttp.status==200){
        alert(xmlHttp.responseText);
      }
    }
    xmlHttp.open('POST','ajax_call.php',true);
    xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlHttp.send(document.getElementById('username').value,document.getElementById('password').value);
  }
  </script>

我的 ajax_call.php 脚本很简单

<?
echo 'hello';
?>

我没有收到警报Hello。为什么?

[我试图提醒readyStatestatus

xmlHttp.onreadystatechange=function(){
      alert(xmlHttp.readyState+" "+xmlHttp.status);
      if(xmlHttp.readyState==4&&xmlHttp.status==200){
        alert(xmlHttp.responseText);
      }
    }

并依次得到关注

1 0
2 200
3 200
4 200
hello
1 0
hello
4

3 回答 3

0
login()
      {
        try{
          xmlHttp=new XMLHttpRequest();
        }catch(e){
          try{
          xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }catch(e2){
          try{
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
          }catch(e3){

          alert('Sorry ! AJAX not supported');Or
        }
        }
        }
        xmlHttp.onreadystatechange=function(){
          if(xmlHttp.readyState==4&&xmlHttp.status==200){
            alert(xmlHttp.responseText);
          }
        }
        xmlHttp.open('POST','ajax_call.php',true);
        xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");/* Anil Remove ;*/
        xmlHttp.send('username'.value,'password'.value);/* try static username and password*/
      }
      </script>[ajax call][1]


  [1]: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_post2
于 2013-06-28T13:12:54.747 回答
0

最后一行应该是

xmlHttp.send();

但看起来你想做一些身份验证......如果是基本身份验证,值应该放在标题中,或者如果它是服务器身份验证的一些孩子,它取决于你的实现......你能解释一下为什么你有xmlHttp.send() 调用中的用户名/密码?

于 2013-06-28T13:13:06.373 回答
0

只是一个想法:

  • 我认为您必须在onreadystatechange之前打开您的 xmlHttp !

.open 的第三个参数告诉 xmlHttp 触发 onreadystatechange(这就是它设置为 true 的原因)

但是,如果这不是错误:

  • 你试过this.responseText吗?
于 2013-06-28T13:22:12.293 回答