簡體   English   中英

需要幫助來捕獲AJAX響應

[英]Need help to catch AJAX response

我在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>

我的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
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();

但是看起來您想進行某種身份驗證...如果是基本身份驗證,則值應放在標頭中,或者如果它是服務器身份驗證的子類,則取決於您的實現...您能否解釋為什么要使用您的xmlHttp.send()調用中的用戶名/密碼?

只是一個想法:

  • 我認為您必須 onreadystatechange 之前 打開 xmlHttp!

.open的第三個參數告訴xmlHttp觸發onreadystatechange(這就是為什么將其設置為true的原因)

但是,如果那不是錯誤:

  • 您嘗試過this.responseText嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM