簡體   English   中英

使用XMLHttpRequest的響應

[英]Use response of XMLHttpRequest

我是JS的新手,正在嘗試創建一個在提交表單之前檢查某些內容的函數。 在html上,我有:

<form name="loginform" method="post" onsubmit="return foo()" action="adminlogin.php"> ... </form>

函數foo()是:

foo(){
  if(..){
  return false;
  }
  else{
        var url = "credenziali.php";
        var params = "name="+username+"&password="+password;
        var http_r = new XMLHttpRequest();
        http_r.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                //something
            }
        };
        http_r.open("GET", url+"?name="+username+"&pass="+password, true);
        http_r.send();
        // here I want to return false or true dependence of responseText
    }

函數中的注釋是我要使用請求返回的內容。 我能怎么做?

嘗試將其插入http_r.send()之前,並在其中插入返回值。

 http_r.onload = function() { if (http_r.status >= 200 && http_r.status < 400) { // Success! } else { // We reached our target server, but it returned an error } }; 

改用JQuery AJAX ... http://api.jquery.com/jquery.ajax/更為簡潔。

暫無
暫無

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

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