<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>登录案例 联动后端服务器</title> <script type="text/javascript" src="js/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("#btn").on("click",function(){ var username = $("#username").val(); var password = $("#password").val(); $.ajax({ url:"http://localhost:8080/ajax", data:{ username:username, password:password }, type:"post", dataType:"json", async:true,//false使用同步,true表示异步 success:function(req){ if (req.success==1) { console.log(req.user); alert('登陆成功!'); $(location).attr('href','index.html'); } else { alert('登录失败!'); $(location).attr('href','user.html'); } }, error:function(xmlHttp,errorText){ console.log(xmlHttp); console.log(errorText); alert("请求异常,请联系管理员解决!"); $(location).attr('href','user.html'); } }) }); }); </script></head><body> <label>姓名:</label> <input type="text" id="username" name="username"> <br> <label>密码:</label> <input type="password" id="password" name="password"> <br> <input type="button" id="btn" name="btn" value="登录"></body></html>