简体   繁体   English

使用XMLHttpRequest的响应

[英]Use response of XMLHttpRequest

I'm new in JS and I'm trying to create a function that check something before submit a form. 我是JS的新手,正在尝试创建一个在提交表单之前检查某些内容的函数。 On html I have: 在html上,我有:

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

The function foo() is: 函数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
    }

the comment in the function is where I want use the what the request returns. 函数中的注释是我要使用请求返回的内容。 How can I do? 我能怎么做?

Try insert this before http_r.send() with your return inside. 尝试将其插入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