软件系统开发定制Ajax:ajax发送Post请求、ajax案例


目录:

(1)ajax发送Post请求

(2)ajax发送post软件系统开发定制请求模拟提交表单数据

(3)ajax软件系统开发定制案例验证用户名是否可用

(4)发送ajax软件系统开发定制请求动态展示学生列表案例


 

(1)ajax发送Post请求

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>发送ajax post请求</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. window.onload = function () {
  10. document.getElementById("mybtn").onclick = function () {
  11. // 发送AJAX POST请求
  12. // 1. 创建AJAX核心对象
  13. var xhr = new XMLHttpRequest();
  14. // 2. 软件系统开发定制注册回调函数
  15. xhr.onreadystatechange = function (){
  16. if (this.readyState == 4) {
  17. if (this.status == 200) {
  18. document.getElementById("mydiv").innerHTML = this.responseText
  19. }else{
  20. alert(this.status)
  21. }
  22. }
  23. }
  24. // 3. 开启通道
  25. xhr.open("POST", "/ajax/ajaxrequest3", true)
  26. // 4. 发送请求
  27. xhr.send()
  28. }
  29. }
  30. </script>
  31. <button id="mybtn">发送AJAX POST请求</button>
  32. <div id="mydiv"></div>
  33. </body>
  34. </html>

AjaxRequest3Servlet:out软件系统开发定制返回的数据回被浏览器:XMLHttpRequest对象获取

  1. package com.bjpowernode.ajax.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. @WebServlet("/ajaxrequest3")
  10. public class AjaxRequest3Servlet extends HttpServlet {
  11. @Override
  12. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. response.setContentType("text/html;charset=UTF-8");
  15. PrintWriter out = response.getWriter();
  16. out.print("<font color='red'>软件系统开发定制用户名已存在!!!</font>");
  17. }
  18. }

 (2)ajax发送post请求模拟提交表单数据

ajaxPost.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>发送ajax post请求</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. window.onload = function () {
  10. document.getElementById("mybtn").onclick = function () {
  11. // 发送AJAX POST请求
  12. // 1. 创建AJAX核心对象
  13. var xhr = new XMLHttpRequest();
  14. // 2. 注册回调函数
  15. xhr.onreadystatechange = function (){
  16. if (this.readyState == 4) {
  17. if (this.status == 200) {
  18. document.getElementById("mydiv").innerHTML = this.responseText
  19. }else{
  20. alert(this.status)
  21. }
  22. }
  23. }
  24. // 3. 开启通道
  25. xhr.open("POST", "/ajax/ajaxrequest3", true)
  26. // 4. 发送请求
  27. // 怎么模拟AJAX提交form表单呢?设置请求头的内容类型(这行代码非常关键,是模拟form表单提交的关键代码。)
  28. // 设置请求头的内容类型时,必须在open之后。
  29. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  30. // 放到send()这个函数的小括号当中的数据,会自动在请求体当中提交数据。
  31. // 使用JS代码获取用户填写的用户名和密码
  32. var username = document.getElementById("username").value;
  33. var password = document.getElementById("password").value;
  34. //xhr.send("注意格式:放在这里的数据就是在请求体当中提交的,格式不能随便来,还是需要遵循HTTP的协议:name=value&name=value&name=value")
  35. xhr.send("username="+username+"&password="+password)
  36. }
  37. }
  38. </script>
  39. 用户名<input type="text" id="username"><br>
  40. 密码<input type="password" id="password"><br>
  41. <button id="mybtn">发送AJAX POST请求</button>
  42. <div id="mydiv"></div>
  43. </body>
  44. </html>

AjaxRequest3Servlet:

  1. package com.bjpowernode.ajax.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. @WebServlet("/ajaxrequest3")
  10. public class AjaxRequest3Servlet extends HttpServlet {
  11. @Override
  12. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  13. throws ServletException, IOException {
  14. response.setContentType("text/html;charset=UTF-8");
  15. PrintWriter out = response.getWriter();
  16. //out.print("<font color='red'>用户名已存在!!!</font>");
  17. // 获取提交数据
  18. String username = request.getParameter("username");
  19. String password = request.getParameter("password");
  20. out.print("用户名是:" + username + ",密码是:" + password);
  21. }
  22. }

 (3)ajax案例验证用户名是否可用

 ajax5.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>AJAX POST请求验证用户名是否可用</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. window.onload = function(){
  10. //获得焦点,span标签清空
  11. document.getElementById("username").onfocus = function (){
  12. document.getElementById("tipMsg").innerHTML = ""
  13. }
  14. //失去焦点,触发ajax请求
  15. document.getElementById("username").onblur = function (){
  16. //console.log("正在发送AJAX POST请求验证用户名")
  17. // 发送AJAX POST请求
  18. // 1.
  19. var xhr = new XMLHttpRequest()
  20. // 2.
  21. xhr.onreadystatechange = function () {
  22. if (this.readyState == 4) {
  23. if (this.status == 200) {
  24. document.getElementById("tipMsg").innerHTML = this.responseText
  25. }else{
  26. alert(this.status)
  27. }
  28. }
  29. }
  30. // 3.开启通道
  31. xhr.open("POST", "/ajax/ajaxrequest4", true)
  32. // 4.设置表单请求头
  33. xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
  34. // 获取表单数据
  35. var username = document.getElementById("username").value
  36. xhr.send("uname=" + username)
  37. }
  38. }
  39. </script>
  40. 用户名:<input type="text" id="username">
  41. <span id="tipMsg"></span>
  42. </body>
  43. </html>

AjaxRequest4Servlet:

  1. package com.bjpowernode.ajax.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.*;
  10. @WebServlet("/ajaxrequest4")
  11. public class AjaxRequest4Servlet extends HttpServlet {
  12. @Override
  13. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  14. throws ServletException, IOException {
  15. // 获取用户名
  16. String uname = request.getParameter("uname");
  17. // 打布尔标记(一种编程模型)
  18. boolean flag = false; // 默认是用户名不存在。
  19. // 连接数据库验证用户名是否存在
  20. Connection conn = null;
  21. PreparedStatement ps = null;
  22. ResultSet rs = null;
  23. try {
  24. // 1.注册驱动
  25. Class.forName("com.mysql.cj.jdbc.Driver");
  26. // 2.获取连接
  27. conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bjpowernode?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC", "root", "123456");
  28. // 3.获取预编译的数据库操作对象
  29. String sql = "select id,name from t_user where name = ?";
  30. ps = conn.prepareStatement(sql);
  31. ps.setString(1, uname);
  32. // 4.执行SQL语句
  33. rs = ps.executeQuery();
  34. // 5.处理结果集
  35. if (rs.next()) {
  36. // 用户名已存在。
  37. flag = true;
  38. }
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. } finally {
  42. // 6.释放资源
  43. if (rs != null) {
  44. try {
  45. rs.close();
  46. } catch (SQLException e) {
  47. e.printStackTrace();
  48. }
  49. }
  50. if (ps != null) {
  51. try {
  52. ps.close();
  53. } catch (SQLException e) {
  54. e.printStackTrace();
  55. }
  56. }
  57. if (conn != null) {
  58. try {
  59. conn.close();
  60. } catch (SQLException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. // 响应结果到浏览器
  66. response.setContentType("text/html;charset=UTF-8");
  67. PrintWriter out = response.getWriter();
  68. if (flag) {
  69. // 用户名已存在,不可用
  70. out.print("<font color='red'>对不起,用户名已存在</font>");
  71. }else{
  72. // 用户名不存在,可以使用
  73. out.print("<font color='green'>用户名可以使用</font>");
  74. }
  75. }
  76. }

 数据库表中的数据:

 (4)发送ajax请求动态展示学生列表案例

 

ajax6.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>发送AJAX请求,显示学生列表</title>
  6. </head>
  7. <body>
  8. <script type="text/javascript">
  9. window.onload = function () {
  10. document.getElementById("btn").onclick = function () {
  11. // 1.创建核心对象
  12. var xhr = new XMLHttpRequest();
  13. // 2.注册回调函数
  14. xhr.onreadystatechange = function () {
  15. if (this.readyState == 4) {
  16. if (this.status == 200) {
  17. document.getElementById("stutbody").innerHTML = this.responseText
  18. } else {
  19. alert(this.status)
  20. }
  21. }
  22. }
  23. // 3.开启通道
  24. xhr.open("GET", "/ajax/ajaxrequest5?t=" + new Date().getTime(), true)
  25. // 4.发送请求
  26. xhr.send()
  27. }
  28. }
  29. </script>
  30. <input type="button" value="显示学员列表" id="btn">
  31. <table width="50%" border="1px">
  32. <thead>
  33. <tr>
  34. <th>序号</th>
  35. <th>姓名</th>
  36. <th>年龄</th>
  37. <th>住址</th>
  38. </tr>
  39. </thead>
  40. <tbody id="stutbody">
  41. <!--<tr>
  42. <td>1</td>
  43. <td>张三</td>
  44. <td>20</td>
  45. <td>北京大兴区</td>
  46. </tr>
  47. <tr>
  48. <td>2</td>
  49. <td>李四</td>
  50. <td>22</td>
  51. <td>北京海淀区</td>
  52. </tr>-->
  53. </tbody>
  54. </table>
  55. </body>
  56. </html>

AjaxRequest5Servlet:

  1. package com.bjpowernode.ajax.servlet;
  2. import javax.servlet.ServletException;
  3. import javax.servlet.annotation.WebServlet;
  4. import javax.servlet.http.HttpServlet;
  5. import javax.servlet.http.HttpServletRequest;
  6. import javax.servlet.http.HttpServletResponse;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.*;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. @WebServlet("/ajaxrequest5")
  13. public class AjaxRequest5Servlet extends HttpServlet {
  14. @Override
  15. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  16. // 连接数据库,查询学员信息,拼接HTML代码,响应HTML代码到浏览器(这里就不再连接数据库了,写死了。)
  17. response.setContentType("text/html;charset=UTF-8");
  18. PrintWriter out = response.getWriter();
  19. // 不连接数据库,拼接HTML代码
  20. StringBuilder html = new StringBuilder();
  21. html.append("<tr>");
  22. html.append("<td>1</td>");
  23. html.append("<td>王五</td>");
  24. html.append("<td>20</td>");
  25. html.append("<td>北京大兴区</td>");
  26. html.append("</tr>");
  27. html.append("<tr>");
  28. html.append("<td>2</td>");
  29. html.append("<td>李四</td>");
  30. html.append("<td>22</td>");
  31. html.append("<td>北京海淀区</td>");
  32. html.append("</tr>");
  33. out.print(html);
  34. }
  35. }

点击按钮:

 上面项目,目前存在的缺点:在后端的java代码中又开始拼接HTML代码了。显然这是在后端java中写前端的HTML代码。不好维护。一般后端不会这样写拼html串返回,只会在后端查数据,把数据返回交给前端

能不能直接将数据返回,给WEB前端数据就行了。让WEB前端能够拿到数据就行,然后页面展示的功能交给WEB前端来处理。

我们后端的java代码能不能只返回数据????可以。(返回数据可以采用JSON的格式,也可以采用XML的格式)

一般采用JSON,因为json体积小,xml体积大,解析起来有一定难度。

网站建设定制开发 软件系统开发定制 定制软件开发 软件开发定制 定制app开发 app开发定制 app开发定制公司 电商商城定制开发 定制小程序开发 定制开发小程序 客户管理系统开发定制 定制网站 定制开发 crm开发定制 开发公司 小程序开发定制 定制软件 收款定制开发 企业网站定制开发 定制化开发 android系统定制开发 定制小程序开发费用 定制设计 专注app软件定制开发 软件开发定制定制 知名网站建设定制 软件定制开发供应商 应用系统定制开发 软件系统定制开发 企业管理系统定制开发 系统定制开发