12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>form 表单页面</title>
- </head>
- <body>
-
- <form id="myForm" action="http://localhost:3000/post/index" method="post" style="width: 245px;">
- <div>
- <label>用户名:
- <input type="text" name="iptuname" id="uname" />
- </label>
- </div><br/>
- <div>
- <label>密码:
- <input type="password" name="iptpwd" id="pwd" />
- </label>
- </div><br/>
- <button type="button" id="btn">检测用户是否存在</button><br/>
- <input type="submit" value="发送 POST 请求" /><br/>
- </form>
- <script>
- window.onload = function () {
-
- document.getElementById('myForm').onsubmit = function () {
- return false;
- }
-
- document.getElementById('btn').addEventListener("click", function () {
- var xhr = new XMLHttpRequest();
- xhr.open("POST", "http://localhost:3000/post", true);
-
- xhr.setRequestHeader("Content-Type", "application/json");
-
- var uname = document.getElementById('uname').value;
- var pwd = document.getElementById('pwd').value;
-
- var data = JSON.stringify({
- iptuname: uname,
- iptpwd: pwd
- });
-
- xhr.send(data);
-
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4) {
- if (xhr.status === 200) {
- var obj = JSON.parse(xhr.responseText);
- console.log("响应数据: ", obj);
- alert("返回的用户名: " + obj.uname);
- } else {
- console.log("请求失败: " + xhr.status);
- }
- }
- }
- });
- }
- </script>
- </body>
- </html>
|