当前位置: 首页 > news >正文

网站开发案例网站开发研究方法

网站开发案例,网站开发研究方法,什么样的网站好优化,苍溪县建设局网站Axios 对原生的AJAX进行封装&#xff0c;简化书写。 Axios官网是&#xff1a;https://www.axios-http.cn 2.1 基本使用 axios 使用是比较简单的&#xff0c;分为以下两步&#xff1a; 引入 axios 的 js 文件 <script src"js/axios-0.18.0.js"></script…

Axios 对原生的AJAX进行封装,简化书写。

Axios官网是:https://www.axios-http.cn

2.1 基本使用

axios 使用是比较简单的,分为以下两步:

  • 引入 axios 的 js 文件

<script src="js/axios-0.18.0.js"></script>

使用axios 发送请求,并获取响应结果

  • 发送 get 请求

axios({method:"get",url:"http://localhost:8080/ajax-demo1/aJAXDemo1?username=zhangsan"
}).then(function (resp){alert(resp.data);
})
  • 发送 post 请求
axios({method:"post",url:"http://localhost:8080/ajax-demo1/aJAXDemo1",data:"username=zhangsan"
}).then(function (resp){alert(resp.data);
});

axios() 是用来发送异步请求的,小括号中使用 js 对象传递请求相关的参数:

  • method 属性:用来设置请求方式的。取值为 get 或者 post

  • url 属性:用来书写请求的资源路径。如果是 get 请求,需要将请求参数拼接到路径的后面,格式为: url?参数名=参数值&参数名2=参数值2

  • data 属性:作为请求体被发送的数据。也就是说如果是 post 请求的话,数据需要作为 data 属性的值。

then() 需要传递一个匿名函数。我们将 then() 中传递的匿名函数称为 回调函数,意思是该匿名函数在发送请求时不会被调用,而是在成功响应后调用的函数。而该回调函数中的 resp 参数是对响应的数据进行封装的对象,通过 resp.data 可以获取到响应的数据。

2.2 快速入门

2.2.1 后端实现

定义一个用于接收请求的servlet,代码如下:

@WebServlet("/axiosServlet")
public class AxiosServlet extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("get...");//1. 接收请求参数String username = request.getParameter("username");System.out.println(username);//2. 响应数据response.getWriter().write("hello Axios~");}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {System.out.println("post...");this.doGet(request, response);}
}

2.2.2 前端实现

  • 引入 js 文件

<script src="js/axios-0.18.0.js"></script>

发送 ajax 请求

  • get 请求

axios({method:"get",url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"
}).then(function (resp) {alert(resp.data);
})
  • post 请求
axios({method:"post",url:"http://localhost:8080/ajax-demo/axiosServlet",data:"username=zhangsan"
}).then(function (resp) {alert(resp.data);
})

整体页面代码如下:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>Title</title>
</head>
<body><script src="js/axios-0.18.0.js"></script>
<script>//1. get/* axios({method:"get",url:"http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan"}).then(function (resp) {alert(resp.data);})*///2. post  在js中{} 表示一个js对象,而这个js对象中有三个属性axios({method:"post",url:"http://localhost:8080/ajax-demo/axiosServlet",data:"username=zhangsan"}).then(function (resp) {alert(resp.data);})
</script>
</body>
</html>

2.3 请求方法别名

为了方便起见, Axios 已经为所有支持的请求方法提供了别名。如下:

  • get 请求 : axios.get(url[,config])

  • delete 请求 : axios.delete(url[,config])

  • head 请求 : axios.head(url[,config])

  • options 请求 : axios.option(url[,config])

  • post 请求:axios.post(url[,data[,config])

  • put 请求:axios.put(url[,data[,config])

  • patch 请求:axios.patch(url[,data[,config])

而我们只关注 get 请求和 post 请求。

入门案例中的 get 请求代码可以改为如下:

axios.get("http://localhost:8080/ajax-demo/axiosServlet?username=zhangsan").then(function (resp) {alert(resp.data);
});

入门案例中的 post 请求代码可以改为如下:

axios.post("http://localhost:8080/ajax-demo/axiosServlet","username=zhangsan").then(function (resp) {alert(resp.data);
})

http://www.15wanjia.com/news/163641.html

相关文章:

  • 福建省高速公路建设管理网站竞价托管收费标准
  • 国外网站做acm题目比较好新产品上市推广策划方案
  • 双峰网站建设网站界面设计如何实现功能美与形式美的统一
  • 公司网站开发服务费属于无形资产哪一类网络运营是什么意思
  • 上海门户网站制作公司做网站需要看那几点
  • 河南省专业做网站公司大数据是干什么的
  • 网站小白怎么开始学网站建设企业建站多少钱一个月
  • 义乌网站制作公司音乐网站的设计与开发
  • wordpress二级目录伪静态太原seo顾问
  • 找人做个网站建设制作报价方案最简单的wordpress主题
  • 做ipad的网站尺寸是多少钱wordpress前台发帖
  • 德清网站制作哪个app推广佣金高
  • 集团品牌网站建设北京网站快速优化排名
  • 中国空间站图片兰州工业发展建设有限公司网站
  • 青岛做网站的大公司有网址大全是ie浏览器吗
  • 做网站用啥软件好wordpress是博客
  • 长沙建站模板深圳百度百科
  • 莱芜正规的企业建站公司wordpress 表单录入
  • 网站建设工作情况公司建网
  • 重庆专业网站开发服务海外推广什么意思
  • 天猫秘券网站怎么做网络营销学校
  • 商城展示网站网站建设 售后服务
  • 网站内容策划方案电商推广方案
  • wordpress知识付费网站设计影响seo的因素
  • 有网站地图的网站wordpress会员等级下载
  • 搭建网站有费用吗太原建网站公司
  • 北京网站建设策划排名如何做蛋糕
  • 村网站建设计划书河南省城乡与住房建设厅网站
  • 怎么恢复网站数据库文件位置天水网站seo
  • 网站建设需要经历什么步骤外贸平台大全