简体   繁体   中英

form submit vs separate XMLHttpRequest

I'm working with Node.js and have express as my main framework.

I have a login webpage with the following code.

  <script>
  function sendResponse(){
    var data = new FormData();
    var login = document.getElementById("login").value;
    var password = document.getElementById("password").value;
    data.append('login', login);
    data.append('password', password);

    var xhr = new XMLHttpRequest();
    xhr.open('POST', 'http://myURL/login', true);
    xhr.onload = function () {
      // do something to response
      var result = JSON.parse(this.responseText);
      if(result["result"] == "fail"){
        alert("No Account Found. Please Register First.");
        window.location.replace("http://myURL/register");
      }
      else{
        window.location.replace("http://myURL/mainpage");
      }
    };
    xhr.send(data);
  }
  </script>

  <h1>Login Portal</h1>
  <hr />
  <form method="post">
        <table>
               <tr>
                   <td><label>Username or Email:</label></td>
                   <td><input id="login" type="text" name="login" /></td>
               </tr>
               <tr>
                   <td><label>Password:</label></td>
                   <td><input id="password" type="password" name="password" /></td>
               </tr>
        </table>
    <button type="button" onclick="sendResponse()">Login</button>
    </form>

Node.js can't send two responses for one request.

The html is not sent by a normal FORM-SUBMIT request but have a separate javascript function that creates a new httprequest instance and gathers info from the form and send it to the server, get the response back and do further actions from there (to register or the mainpage).

What my friend whats to do is make this a normal FORM-SUBMIT type with the action property targeted to the login url, and since I can only send one response, I would send a redirect and take control of the route-handling. But then since the client won't have other member data like before he would send another request through a separate route to get the logged-in member's data such as the session and the member's key.

When I look at many famous websites, the html code does implement what my friend described, and he wants to make the website through the standard procedures. He think my way of processing requests through an onclick trigger is a workaround. I get it, and I'm not the familiar with web developing, I'm in charge of the server. But doesn't this make the workload x2? I mean, if the client side can deal with post actions once it gets the data, isn't that not a workaround but a normal way of handling requests?

Node.js can't send two responses for one request

I don't think any web server can do this, unless you're using a duplex connection, like a websocket.

He think my way of processing requests through an onclick trigger is a workaround.

Either that or the submit event of the form. It's very versatile and is supported by pretty much any browser out rhere.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM