Php 需要帮助捕获AJAX响应吗

Php 需要帮助捕获AJAX响应吗,php,ajax,Php,Ajax,我在我的home.php中编写了以下代码。单击提交按钮调用login()函数 function login() { try{ xmlHttp=new XMLHttpRequest(); }catch(e){ try{ xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e2){ try{ xmlHttp=new ActiveXObject("Ms

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

 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>
并按顺序得到了跟踪

1 0
2 200
3 200
4 200
hello
1 0
hello
login()
{
试一试{
xmlHttp=新的XMLHttpRequest();
}捕获(e){
试一试{
xmlHttp=新的ActiveXObject(“Microsoft.xmlHttp”);
}渔获物(e2){
试一试{
xmlHttp=新的ActiveXObject(“Msxml2.xmlHttp”);
}渔获物(e3){
警报(“抱歉!不支持AJAX”);或
}
}
}
xmlHttp.onreadystatechange=函数(){
if(xmlHttp.readyState==4&&xmlHttp.status==200){
警报(xmlHttp.responseText);
}
}
open('POST','ajax_call.php',true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);/*Anil-Remove*/
发送('username'.value,'password'.value);/*尝试静态用户名和密码*/
}
[ajax call][1]
[1]: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_post2

最后一行应该是

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

只是一个想法:

  • 我认为在更改ReadyState之前,您必须打开您的xmlHttp
.open的第三个参数告诉xmlHttp激发onreadystatechange (这就是为什么它被设置为true)

但是,如果这不是错误:

  • 您是否尝试过此。responseText

尝试从xmlHttp.send()中删除参数@Ankit:尝试过但没有帮助所以嗯。。。这不意味着你的“你好”出现了吗?这就是应该发生的事情,每次请求发生变化时,您都会得到更新,从1200(发送,无状态代码)到2200(接收,状态正常),最后到4200(请求就绪,状态正常),然后您会看到您想要的“你好”。。。是吗?@CerylWiltink:但是浏览器没有显示Hello。chrome和firefox都没有。嗯,奇怪的是,我只是准确地复制了你的代码,只是把php脚本的位置改成了一个我制作的php文件来回显“Hello”。我得到的警报很好。。。你能解释一下什么事情没有发生吗?是否有警报弹出窗口但没有内容?或者根本没有警报?下面是一个如何添加基本身份验证标题的示例顺便说一句:我以前使用过本教程,并使其正常工作了…:我从登录表单中选择值,然后使用AJAX记录用户。但我在这里输入“hello”只是为了测试它。
1 0
2 200
3 200
4 200
hello
1 0
hello
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
xmlHttp.send();