Ajax Code:
$(function () {
var $register = $(".register");
var $login = $(".login");
$login.find(".submit").on("click",function () {
});
$register.find("button").on("click", function(){
// ajax
$.ajax({
type:"post",
url:"/api/user/register",
data:{
username: $register.find("[name="username"]").val(),
password: $register.find("[name="password"]").val(),
repassword: $register.find("[name="repassword"]").val(),
email: $register.find("[name="email"]").val()
},
dataType:"json",
success: function (result) {
console.log(result);
}
});
})
})
backend code:
/**
* Created by Administrator on 2018/4/16.
*/
var express = require("express");
var router = express.Router();
//
var responseData;
router.use(function (req, res, next) {
responseData = {
code: 0,
message:""
}
next();
});
/*
*
*
*
* 1.
* 2.
* 3.
*
* 1.
*
* */
router.post("/user/register", function (req, res, next) {
var username = req.body.username;
var password = req.body.password;
var repassword = req.body.repassword;
var email = req.body.email;
console.log(username,password,repassword,email);
//
if(username == ""){
responseData.code = 1;
responseData.message = "";
res.json(responseData);
return;
}
//
if(password == ""){
responseData.code = 2;
responseData.message = "";
res.json(responseData);
return;
}
//
if(password != repassword){
responseData.code = 3;
responseData.message = "";
res.json(responseData);
return;
}
responseData.message = "";
res.json(responseData);
});
module.exports = router;
app.js:
/**
* Created by Administrator on 2018/4/16.
*
*/
//express
var express = require("express");
//
var swig = require("swig");
//
var mongoose = require("mongoose");
//body-parserpost
var bodyParser = require("body-parser");
//app=>NodeHS Http.createServer()
var app = express();
//
//url/public__dirname + "/public"
app.use("/public", express.static(__dirname + "/public"));
// app.use("/static", express.static("public"));
//
//
//
app.engine("html", swig.renderFile);
//views
app.set("views", "./views");
/*view engineapp.engine*/
app.set("view engine", "html");
//
swig.setDefaults({cache: false});
//bodyparser
app.use( bodyParser.urlencoded({extended:true}) );
/*
*
* */
app.use("/admin", require("./routers/admin"));
app.use("/api", require("./routers/api"));
app.use("/", require("./routers/main"));
//http
mongoose.connect("mongodb://localhost:27017/blog",function (err) {
if(err){
console.log("");
}else{
console.log("");
app.listen(8081);
}
});
HTML Code:
<form method="post" action="" class="register">
<div class="inset-register">
<label for="username">:</label><input type="text" id="username" name="username">
</div>
<div class="inset-register">
<label for="password">:</label><input type="password" id="password" name="password">
</div>
<div class="inset-register">
<label for="repassword">:</label><input type="password" id="repassword" name="repassword">
</div>
<div class="inset-register">
<label for="email">:</label><input type="email" id="email" name="email">
</div>
<div class="inset-register">
<button ></button>
</div>
</form>
error reporting problem: