1前台文件
<!doctype html> <html> <head> <meta charset="UTF-8" /> <title>Document</title> <script typet="text/javascript" src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> <style> form{padding: 100px;border: 1px solid red;width: 350px;margin: 0 auto;} form input{display: block;margin: 0 auto;margin-bottom: 20px;} </style> <script> $(function(){ $('#register-sub').on('click',function(){ var info = $('form').serialize(); $.ajax({ type:"get", url:"http://127.0.0.1:8081", data:info, success:function(response,status,xhr){ alert(response); localStorage.name = $('input[name="name"]').val(); } }); return false; }); $('#login-sub').on('click',function(){ return false; }); });</script> </head> <body> <form id="register" action="" method="get"> <input type="hidden" name="action" value="register" /> 用 户 名: <input type="text" name="name" placeholder="请输入用户名..." /> 密  码: <input type="password" name="pass" placeholder="请输入密码..." /> 电子邮件:<input type="email" name="email" placeholder="请输入合法邮件名..." /> <input id="register-sub" type="submit" value="注册" /> </form> </body> </html>
2,server.js
var http = require('http');
var url = require('url');
var util = require('util');
var mysql = require('./mysql');//这个是自己写的用来向数据库写入用户的文件
http.createServer(function(req,res){
res.writeHead(200,{"Content-Type":'text/plain','charset':'utf-8','Access-Control-Allow-Origin':'*','Access-Control-Allow-Methods':'PUT,POST,GET,DELETE,OPTIONS'});
//解析URL参数
var params = url.parse(req.url,true).query;
var a=mysql.reg(params.action,params.name,params.pass,params.email);
var rr="safsaf"
res.write("注册成功");
res.end();
}).listen(8081);
3,mysql.js
// var http = require('http'); // http.createServer(function (request, response) { // // 发送 HTTP 头部 // // HTTP 状态值: 200 : OK // // 内容类型: text/plain // response.writeHead(200, {'Content-Type': 'text/plain'}); // var mysql = require('mysql'); // var connection = mysql.createConnection({ // host : 'localhost', // user : 'root', // password : 'root', // port: '3306', // database: 'test', // }); // // body... // connection.connect(); // var sql = 'SELECT * FROM yueqi'; // //查 // connection.query(sql,function (err, result) { // if(err){ // console.log('[SELECT ERROR] - ',err.message); // return; // } // // console.log('--------------------------SELECT----------------------------'); // // console.log(result); // // console.log('------------------------------------------------------------\n\n'); // }); // console.log(result); // // return res.jsonp(result); // response.end(result); // connection.end(); // // 发送响应数据 "Hello World" // }).listen(8888); exports.reg = function (action,name,pass,email){ var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'localhost', user : 'root', password : 'root', port: '3306', database: 'test', }); connection.connect(); var modSql = "insert into user (id,username,userpwd) values ('','"+name+"','"+pass+"')"; connection.query(modSql,function (err, result) { if(err){ console.log('[UPDATE ERROR] - ',err.message); return; } }); connection.end(); }
=====================================================================================================================================
以上是get方式的,
post方式修改一下前台文件为post就可以了