执行步骤
引入包
axios.min.js · moonshuo/fruit stand - Gitee.com
配置信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>01.演示Axios发送普通的参数值给服务器端</title> <script language="JavaScript" src="script/vue.js"></script> <script language="JavaScript" src="script/axios.min.js"></script> <script language="JavaScript"> window.onload=function(){ var vue = new Vue({ "el":"#div0", data:{ uname:"lina", pwd:"ok" }, methods:{ axios01:function(){ axios({ method:"POST", url:"axios01.do", 在这里定义参数 params:{ uname:vue.uname, pwd:vue.pwd } }) .then(function (value) { console.log(value); }) .catch(function (reason) { console.log(reason); }); } } }); } </script> </head> <body> <div id="div0"> 双向绑定,与vue中的uname和pwd相互绑定 uname:<input type="text" v-model="uname"/><br/> pwd:<input type="text" v-model="pwd"/><br/> //点击之后调用axios01函数对象 <input type="button" @click="axios01" value="发送一个带普通请求参数值的异步请求"/> </div> </body> </html>
|
后台响应
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| @WebServlet("/axios01.do") public class Axios01Servlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8");
String uname = request.getParameter("uname"); String pwd = request.getParameter("pwd");
System.out.println("uname = " + uname); System.out.println("pwd = " + pwd);
response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.write(uname+"_"+pwd);
throw new NullPointerException("这里故意抛出一个空指针异常...."); } }
|
客户端发送JSON格式
JSON表示一个数据格式,比如json表示两个学员的信息
[{sid:”s001”,age:18},{sid:”s002” ,age:18}],JSON表达的数据更加简介,更加可以节约网络带宽,而其与原来的差别只是将method中的date转换成了JSON格式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| methods:{ axios01:function(){ axios({ method:"POST", url:"axios01.do", data:{ uname:vue.uname, pwd:vue.pwd } }) .then(function (value) { console.log(value); }) .catch(function (reason) { console.log(reason); }); } }
|
获取参数的方法
而json格式我们使用request.getParameter进行接受,同时需要导入JSON的jar包,进行格式转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @WebServlet("/axios02.do") public class Axios02Servlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StringBuffer stringBuffer = new StringBuffer(""); BufferedReader bufferedReader = request.getReader(); String str = null ; while((str=bufferedReader.readLine())!=null){ stringBuffer.append(str); } str = stringBuffer.toString() ;
Gson gson = new Gson();
User user = gson.fromJson(str, User.class);
System.out.println(user); } }
|
后台发送JSON数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| @WebServlet("/axios03.do") public class Axios03Servlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
StringBuffer stringBuffer = new StringBuffer(""); BufferedReader bufferedReader = request.getReader(); String str = null ; while((str=bufferedReader.readLine())!=null){ stringBuffer.append(str); } str = stringBuffer.toString() ;
Gson gson = new Gson();
User user = gson.fromJson(str, User.class); user.setUname("鸠摩智"); user.setPwd("123456");
String userJsonStr = gson.toJson(user); response.setCharacterEncoding("UTF-8"); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(userJsonStr); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <script language="JavaScript"> window.onload=function(){ var vue = new Vue({ "el":"#div0", data:{ uname:"lina", pwd:"ok" }, methods:{ axios03:function(){ axios({ method:"POST", url:"axios03.do", data:{ uname:vue.uname, pwd:vue.pwd } }) .then(function (value) { var data = value.data; vue.uname=data.uname; vue.pwd=data.pwd;
}) .catch(function (reason) { console.log(reason); }); } } }); } </script>
|
js与字符串与对象之间的相互转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>04.JS中的字符串和Object之间互转的API</title> <script language="JavaScript"> function hello01(){
var user = {"uname":"lina","age":99}; alert(typeof user); var userStr = JSON.stringify(user); alert(typeof userStr); alert(userStr); } </script> </head> <body> <div id="div0"> <input type="button" value="确定" onclick="hello01()"/> </div> </body> </html>
|
(来自于尚硅谷|尚硅谷丨2022版JavaWeb教程(全新技术栈,全程实战)_哔哩哔哩_bilibili)