跨域访问
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>登录</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no">
<meta name="renderer" content="webkit">
<meta http-equiv="Cache-Control" content="no-siteapp" />
<link rel="stylesheet" href="static/css/amazeui.css" />
<link href="static/css/dlstyle.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="static/css/bootstrap.min.css" />
</head>
<body>
<div class="login-boxtitle" style="height: 100px;">
<div class="logoBig">
<img src="static/images/logo.png" style="margin-left: 0px ; height: 100px;">
</div>
</div>
<div class="login-banner" >
<div class="login-main" >
<div class="login-banner-bg"><span></span><img src="static/images/big.jpg" /></div>
<div class="login-box" style="margin-top: 20px;">
<h3 class="title">登录商城</h3>
<div class="clear"></div>
<div class="login-form" style="background: none; margin-top: 15px;">
<form>
<div class="user-name" style="margin-top: 20px;">
<label for="user"><span class="glyphicon glyphicon-user" aria-hidden="true"></span></label>
<input type="text" v-model="username" id="userName" placeholder="邮箱/手机/用户名">
</div>
<div class="user-pass" style="margin-top: 20px;">
<label for="password"><span class="glyphicon glyphicon-lock" aria-hidden="true"></span></label>
<input type="password" v-model="password" id="userPwd" placeholder="请输入密码">
</div>
</form>
</div>
<div class="login-links" >
<label for="remember-me"><input id="remember-me" type="checkbox">记住密码</label>
<a href="#" class="am-fr">忘记密码</a>
<br />
</div>
<div class="am-cf">
<input type="submit" id="submitBtn" value="登 录" class="am-btn am-btn-primary am-btn-sm">
</div>
<div class="am-cf">
<input type="button" onclick="javascript:window.location.href='register.html'" value="注 册" class="am-btn am-btn-default am-btn-sm">
</div>
<div class="partner">
</div>
</div>
</div>
</div>
<div class="footer ">
<div class="footer-hd ">
</div>
<div class="footer-bd ">
<p>
<a href="# ">联系我们</a>
<a href="# ">网站地图</a>
</p>
</div>
</div>
<script type="text/javascript" src="static/js/vue.js"></script>
<script type="text/javascript" src="static/js/axios.min.js"></script>
<script type="text/javascript" src="static/js/jquery-1.9.min.js"></script>
<script type="text/javascript">
$("#submitBtn").click(function(){
var name=$("#userName").val();
var pwd=$("#userPwd").val();
$.get("http://localhost:8080/user/login",{username:name,password:pwd},function(res){
console.log(res);
},"json")
});
</script>
</body>
</html>
解决跨域访问。需要在UserController类上加上@CrossOrigin注解
package com.qfedu.fmmall.controller;
import com.qfedu.fmmall.service.UserService;
import com.qfedu.fmmall.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@ResponseBody
@Controller
@RequestMapping("/user")
@Api(value="提供用户的登录和注册接口",tags="用户管理")
@CrossOrigin
public class UserController {
@Resource
private UserService userService;
@ApiOperation("用户登录接口")
@ApiImplicitParams({
@ApiImplicitParam(dataType = "string",name = "username",value="用户登录账号",required = true),
@ApiImplicitParam(dataType = "string",name = "password",value="用户登录密码",required = true)
})
@RequestMapping(value = "/login",method= RequestMethod.GET)
public ResultVO login(@RequestParam("username") String name,@RequestParam(value="password") String pwd){
ResultVO resultVO = userService.checkLogin(name, pwd);
return resultVO;
}
@ApiOperation("用户注册接口")
@ApiImplicitParams({
@ApiImplicitParam(dataType = "string",name = "username",value="用户注册账号",required = true),
@ApiImplicitParam(dataType = "string",name = "password",value="用户注册密码",required = true)
})
@PostMapping( "/regist")
public ResultVO regist(String username,String password){
return userService.userRegist(username,password);
}
}
账号,密码校检代码
<script type="text/javascript">
$("#submitBtn").click(function(){
var name=$("#userName").val();
var pwd=$("#userPwd").val();
if(name.length<8||name.length>20){
$("#tips").html("<label style='color:red'>账号长度必须为8-20个字符</label>")
}else{
if(pwd.length<8||pwd.length>16){
$("#tips").html("<label style='color:red'>密码长度必须为8-16个字符</label>")
}else{
$.get("http://localhost:8080/user/login",{username:name,password:pwd},function(res){
console.log(res);
if(res.code==10000){
window.location.href="index.html";
}else{
$("#tips").html("<label style='color:red'>"+res.msg+"</label>");
}
},"json")
}
}
});
</script>
前端页面cookie传值
var userInfo=res.data;
document.cookie="username="+userInfo.username;
document.cookie="userImg"+userInfo.userImg;
window.location.href="index.html";
cookie_utils.js
var operator="="
function getCookieValue(keyStr){
var value=null;
var s=window.document.cookie;
var arr=s.split("; ");
var keyStr;
var value;
for(var i=0;i<arr.length;i++){
var str=arr[i];
var k=str.split("=")[0];
var v=str.split("=")[1];
if(k==keyStr){
value=v;
break;
}
}
return value;
}
function setCookieValue(key,value){
document.cookie=key+operator+value;
}
index.html
<script type="text/javascript" src="static/js/cookie_utils.js"></script>
<script type="text/javascript">
var name=getCookieValue("username");
var img=getCookieValue("userimg");
console.log(name);
console.log(img);
</script>
改进前端页面cookie传值 引入 login.html
<script type="text/javascript" src="static/js/cookie_utils.js"></script>
var userInfo=res.data;
//document.cookie="username="+userInfo.username;
//document.cookie="userImg"+userInfo.userImg;
setCookieValue("username",userInfo.username);
setCookieValue("userimg",userInfo.userImg);
登录后控制台显示
|