This article describes how to use Node. js as the background and jQuery as the front-end AJAX code to Implement User Login and registration functions. It has good reference value. Next, let's take a look at it. This article mainly introduces the knowledge of using Node. js as the background and jQuery as the front-end AJAX code to Implement User Login and registration functions. It has good reference value. Let's take a look at it with the small Editor.
Recently, Node. js was implemented as the backend. jQuery writes the front-end AJAX code to implement the user login and registration functions, refreshing the three views of front-end interaction. Special record.
1. Implement form framework in login. ejs
Form does not add a method. It uses ajax to send requests.
2. js implements request sending
$ ("# Login_submit "). click (function () {var username = $ ('# username '). val (); var password = $ ('# password '). val (); // determine the username and password format here //........ // send an ajax request using the post method to send a json string to the background login $. ajax ({type: "post", url: "http: // localhost: 3000/login", dataType: "json", data: {username: username, password: password}, success: function (data) {// receives the returned data, and the front-end determines the action taken if (data) {if (data. message = "false") {alert ('wrong password, please input again '); window. location. href = "login";} else {alert ('login successful '); window. location. href = "index" ;}} else {}}});});
2. Background reception
/*** Registration page post */router. post ('/reg', function (req, res, next) {// first query whether this user console exists. log ("req. body "+ req. body); user2.findDocuments (DATABASE, "users", 1, {"uName": req. body. username}, function (user) {res. setHeader ('content-type', 'application/json; charset = UTF-8 '); if (user. length = 0) {// the user name does not repeatedly agree to create user2.insertDocuments (DATABASE, "users", [{"uName": req. body. username, "uPasswd": req. body. passwo Rd, "uEmail": req. body. email, "uHasshop": 0, "us%name": "null", "aId": '-1'}], function (result) {res. send ({status: "success", message: "true"}) ;}}else {// duplicate user name. Find this user and disagree with the creation of user res. send ({status: "success", message: "false "});}});});
The registration page is similar to the login page. I will not go into details.
In short, I used to think that the pages accessed by my browser are front-end items. The back-end is a pile of code that is far away. Now we understand that all the URLs accessed belong to the backend. The browser displays what content is sent by the backend. The front-end display is only half past one, and the back-end resources are always great.
The above is a detailed description of user login registration (AJAX interaction) using node. js + jQuery. For more information, see other related articles in the first PHP community!