1. ホーム
  2. Web プログラミング
  3. AJAX関連

ユーザー名の有無を検出するAJAXメソッド

2022-01-15 06:36:36

この例では、参考までにユーザー名の有無を検出するAJAXの具体的なコードを以下のように共有します。

vscodeを使用します。

インデックスを表示します。

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Title</title>
</head>
<body>
 <h2>User Registration</h2>
 <form action="/doreg" method="post">
  <ul>
   <li>username:<input type="text" name="username" id="user"><span id="msg"></ span></li>
   <li>password:<input type="text" name="pwd"></li>
   <li>Confirm password: <input type="text" name="repwd"></li>
   <li><input type="submit" value="register"></li>
  </ul>
 </form>
 <script>
  let user = document.querySelector("#user")
  let msg = document.querySelector("#msg")
  user.onblur = function(){
   // alert("hello")
   // Step 1: Create an ajax object
   let xhr = new XMLHttpRequest(); // xhr means ajax object The state of ajax is 0 at this point
   // console.log(xhr.readyState)
   // Step 2: Establish connection with server get means you need to put the data in the url
   xhr.open("get","/check?username="+user.value) // the state of ajax is 1 at this time
   // console.log(xhr.readyState)
   // Step 3: Send the request
   xhr.send(null); // null means the request body is empty get request's request body are empty post request's request body is not empty
   // Step 4: Get the result of the server response Listen for ajax state changes
   xhr.onreadystatechange = function () { // When the state changes, it will trigger the onreadystatechange event
    // console.log(xhr.readyState); // xhr.readyState gets the state of the ajax object
    if(xhr.readyState === 4 && xhr.status == 200){
     // xhr.responseText gets the data of the server response
     // console.log(xhr.responseText)
     msg.innerHTML = xhr.responseText;
    }
   }

  }

 </script>
</body>
</html>

JSです。

let express = require("express");
let bodyParser = require("body-parser");
let app = express();

// set an alias for the ejs template engine, alias html
app.engine("html",require("ejs"). __express);
app.set("view engine","html");// use the html template engine
// Specify the location of the template
app.set("views",". /views")

// Configure the bodyParser
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:false}))

// Routing
app.get("/",(req,res)=>{
 res.render("reg01"); // render module
})

// Handle registration
app.post("/doreg",(req,res)=>{
 // Get the data passed by the client
 let username = req.body.username.trim();
 let pwd = req.body.pwd.trim();
 let repwd = req.body.repwd.trim();
 // console.log(username,pwd,repwd)

 // simulate the user information from the database
 let users = ["wangcai","xiaoqiang","admin"];
 if(users.find(user=>user===username)){
  res.send("<h1 style='color: red'>Sorry, this username has been registered, please change your username~<a href='/'>Return to registration page</a></h1>")
 }else{
  res.send("<h1 style='color: green'>Congratulations, the username is available~<a href='/'>Return to registration page</a></h1>")
 }
})

app.listen(3000,()=>{
 console.log("server is running on 3000~")
})

以上、本記事の全内容をご紹介しましたが、皆様の学習のお役に立てれば幸いです。また、Script Houseをより一層応援していただければ幸いです。