nodejs实现登陆效果
login.js
let http=require('http');
let mysql=require('mysql');
//登录接口
let server=http.createServer(function(req,res){
res.setHeader("Access-Control-Allow-Origin","*");
res.setHeader('Content-type',"text/json;charset=utf8");
let data='';
req.on('data',function(chunk){
data+=chunk;
});
req.on('end',function(){
console.log(data);
});
res.write('ok');
res.end();//响应结束
});
server.listen(9080);
console.log('the server is running.............');
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<button onclick="login()">登录</button>
<script>
function login() {
let serverURL = "http:/localhost:9080";
let obj={
username:'jack',
age:90
};
fetch(serverURL, {
method: "POST",
body: JSON.stringify(obj),
headers: {
'content-type': 'application/x-www-form-urlencoded'
}
}).then(res => res.text()).then(data => {
console.log(data); //将服务器返回结果显示在控制台
});
}
</script>
</body>
</html>
在浏览器内运行前应先在启动终端
转载:https://blog.csdn.net/weixin_44200575/article/details/102488545
查看评论