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

合肥做网站便宜服务营销案例

合肥做网站便宜,服务营销案例,做生物卷子的网站,昆明网站建设云集创目录 ◆ AJAX 概念和 axios 使用 什么是 AJAX? 怎么发送 AJAX 请求? 如何使用axios axios 函数的基本结构 axios 函数的使用场景 1 没有参数的情况 2 使用params参数传参的情况 3 使用data参数来处理请求体的数据 4 上传图片等二进制的情况…

目录

◆ AJAX 概念和 axios 使用

什么是 AJAX?

怎么发送 AJAX 请求? 

如何使用axios 

axios 函数的基本结构

axios 函数的使用场景

1 没有参数的情况

2 使用params参数传参的情况

3  使用data参数来处理请求体的数据

4  上传图片等二进制的情况

form-serialize 插件


◆ AJAX 概念和 axios 使用

什么是 AJAX?

概念:AJAX 是浏览器与服务器进行数据通信的技术 

怎么发送 AJAX 请求? 

1. 使用 axios [æk‘sioʊs] 库

  • 基于 XMLHttpRequest 封装、代码简单
  •  Vue、React 项目中都会用到 axios

2. 使用 XMLHttpRequest 对象

如何使用axios 

语法:

1. 引入 axios.js

  • 在线引入: https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js        --axios在线库
  • 本地引入:下载好axios文件,使用相对路径引入

2. 使用 axios 函数

axios 函数的基本结构

  • url

这个参数是必须的,里面填的是请求的url地址

  • method

这个参数是可选的,默认请求的是GET方法,参数内容不区分大小写

  • params

这个参数主要是替代以前的字符串拼接的方法,填在params里面的参数会以字符串拼接的方式,将参数凭借到url地址上。但这里会对参数值进行url编码

  • data

这个参数主要是用于接收请求体或者二进制参数,所以如果参数是表单数据或者二进制数据,需要使用data这个对象来接收

  • axios的回调处理--then()函数

这个函数主要是处理axios请求成功后返回的数据数据,里面接收一个回调函数

axios的回调处理--catch()函数

这个函数主要是处理axios请求失败后返回的数据数据,里面接收一个回调函数

axios 函数的使用场景

前提条件:成功的引入了js的依赖后

1 没有参数的情况
//场景1:无参数的请求
//不填请求方法参数,默认是GET请求axios({url: 'http://hmajax.itheima.net/api/province'}).then(function(result){//请求成功调用console.log(result);}).catch(function(error){//请求异常调用console.log(error);})

2 使用params参数传参的情况
//场景2:使用params参数的请求axios({url: 'http://hmajax.itheima.net/api/city',params: {pname: '辽宁省'}}).then(function(result){//请求成功调用console.log(result);}).catch(function(error){//请求异常调用console.log(error);})

使用箭头函数来替代匿名函数

3  使用data参数来处理请求体的数据

当请求方法是post方法时,参数会以请求体的方式向服务器提交

  // 场景3  使用data参数来处理请求体的数据axios({url: 'http://hmajax.itheima.net/api/register',method: 'post',data: {username: 'jack12345',password: '123456'}}).then(result=>{//请求成功调用console.log(result);}).catch(error=>{//请求异常调用console.log(error);})

4  上传图片等二进制的情况
  • 1. 获取图片文件对象
  • 2. 使用 FormData 携带图片文件
  • 3. 提交表单数据到服务器,使用图片 url 网址
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head>
<body><!-- 文件选择元素 --><input type="file" class="upload"><img src="" alt=""><script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script><script>//场景1:无参数的请求// axios({//   url: 'http://hmajax.itheima.net/api/province'// }).then(function(result){//   //请求成功调用//   console.log(result);// }).catch(function(error){//   //请求异常调用//   console.log(error);// })//场景2:使用params参数的请求//  axios({//   url: 'http://hmajax.itheima.net/api/city',//   params: {//     pname: '辽宁省'//   }// }).then(function(result){//   //请求成功调用//   console.log(result);// }).catch(function(error){//   //请求异常调用//   console.log(error);// })// // 场景3  使用data参数来处理请求体的数据// axios({//   url: 'http://hmajax.itheima.net/api/register',//   method: 'post',//   data: {//     username: 'jack12345',//     password: '123456'//   }// }).then(result=>{//   //请求成功调用//   console.log(result);// }).catch(error=>{//   //请求异常调用//   console.log(error);// })//4  上传图片等二进制的情况//1. 获取图片文件document.querySelector('.upload').addEventListener('change',(e)=>{//  console.log(e.target.files); //2. 使用 FormData 携带图片文件const fd = new FormData()// append()  追加元素fd.append('img',e.target.files[0])//使用ajax提交数据axios({url: 'http://hmajax.itheima.net/api/uploadimg',method: 'post',data: fd}).then(result=>{console.log(result);// console.log(result.data.data.url);document.querySelector('img').src = result.data.data.url}).catch(error=>{console.log(error);})})</script>
</body>
</html>

在日常的开发中基本大概是这四种常见的情况,学会用这几种,基本上可以解决问题

form-serialize 插件

作用:快速收集表单元素的值

使用:

  • 1 获取表单对象
  • 2 使用serialize函数,快速收集表单元素的值

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>13.form-serialize插件使用</title><script src="./lib/form-serialize.js"></script>
</head><body><form action="javascript:;" class="example-form"><input type="text" name="uname"><br><input type="text" name="pwd"><br><input type="button" class="btn" value="提交"></form><!-- 目标:在点击提交时,使用form-serialize插件,快速收集表单元素值--><script>//为提交按钮设置监听事件document.querySelector('.btn').addEventListener('click', () => {//使用插件快速获取表单元素值/*** 2. 使用serialize函数,快速收集表单元素的值* 参数1:要获取哪个表单的数据*  表单元素设置name属性,值会作为对象的属性名*  建议name属性的值,最好和接口文档参数名一致* 参数2:配置对象*  hash 设置获取数据结构*    - true:JS对象(推荐)一般请求体里提交给服务器*    - false: 查询字符串*  empty 设置是否获取空值*    - true: 获取空值(推荐)数据结构和标签结构一致*    - false:不获取空值*/const form = document.querySelector('.example-form')const data = serialize(form,{hash:true,empty:true})console.log(data);})</script>
</body></html>


文章转载自:
http://comeback.bwmq.cn
http://invigorative.bwmq.cn
http://hektoliter.bwmq.cn
http://rho.bwmq.cn
http://puppydom.bwmq.cn
http://notornis.bwmq.cn
http://bulgar.bwmq.cn
http://affected.bwmq.cn
http://disassemble.bwmq.cn
http://fireballing.bwmq.cn
http://sirenian.bwmq.cn
http://arm.bwmq.cn
http://ambary.bwmq.cn
http://swede.bwmq.cn
http://triplet.bwmq.cn
http://thermoduric.bwmq.cn
http://learning.bwmq.cn
http://cornute.bwmq.cn
http://scandisk.bwmq.cn
http://ultraradical.bwmq.cn
http://perfectionism.bwmq.cn
http://kinaesthetic.bwmq.cn
http://postwar.bwmq.cn
http://seawall.bwmq.cn
http://domiciled.bwmq.cn
http://puritanize.bwmq.cn
http://drupel.bwmq.cn
http://riparian.bwmq.cn
http://carlowitz.bwmq.cn
http://miserliness.bwmq.cn
http://kwakiutl.bwmq.cn
http://unclaimed.bwmq.cn
http://depollute.bwmq.cn
http://hairsplitter.bwmq.cn
http://turdine.bwmq.cn
http://bandoline.bwmq.cn
http://tubbing.bwmq.cn
http://chanteyman.bwmq.cn
http://dramaturgy.bwmq.cn
http://wearing.bwmq.cn
http://refinement.bwmq.cn
http://overcome.bwmq.cn
http://methoxy.bwmq.cn
http://outrelief.bwmq.cn
http://koestler.bwmq.cn
http://wildcard.bwmq.cn
http://talmudic.bwmq.cn
http://forgettable.bwmq.cn
http://vietnamization.bwmq.cn
http://friend.bwmq.cn
http://casita.bwmq.cn
http://infelicity.bwmq.cn
http://walkathon.bwmq.cn
http://exospherical.bwmq.cn
http://sudsy.bwmq.cn
http://citrullin.bwmq.cn
http://somatotrophin.bwmq.cn
http://caudillo.bwmq.cn
http://desulfur.bwmq.cn
http://thug.bwmq.cn
http://bedu.bwmq.cn
http://gross.bwmq.cn
http://turfen.bwmq.cn
http://unlawful.bwmq.cn
http://childbearing.bwmq.cn
http://verbena.bwmq.cn
http://cameraman.bwmq.cn
http://gitana.bwmq.cn
http://apt.bwmq.cn
http://thyrosis.bwmq.cn
http://gallantry.bwmq.cn
http://pastor.bwmq.cn
http://nosing.bwmq.cn
http://chloracne.bwmq.cn
http://trehalose.bwmq.cn
http://metayer.bwmq.cn
http://challah.bwmq.cn
http://theropod.bwmq.cn
http://mannikin.bwmq.cn
http://imroz.bwmq.cn
http://versifier.bwmq.cn
http://openly.bwmq.cn
http://tear.bwmq.cn
http://malibu.bwmq.cn
http://prearrange.bwmq.cn
http://unsensible.bwmq.cn
http://coasting.bwmq.cn
http://cytomegalic.bwmq.cn
http://dicotyledonous.bwmq.cn
http://mele.bwmq.cn
http://unrepressed.bwmq.cn
http://versicle.bwmq.cn
http://sickbed.bwmq.cn
http://banc.bwmq.cn
http://bradawl.bwmq.cn
http://emp.bwmq.cn
http://speediness.bwmq.cn
http://prescind.bwmq.cn
http://determine.bwmq.cn
http://satanism.bwmq.cn
http://www.hrbkazy.com/news/62146.html

相关文章:

  • 网站建设之织梦模板2023智慧树网络营销答案
  • wordpress仿站上传到搜索引擎优化简历
  • 高端人才做兼职的招聘网站有哪些最好的网络推广方式
  • 郑州网站开发的公司关键词检索
  • 网上三维展馆网站是怎么做的关键词大全
  • 网站备案个人承诺书国际网络销售平台有哪些
  • 网站商务通js代码网站关键词优化有用吗
  • wordpress修改文章阅读量seo5
  • 网站建设运营与维护标准seo排名点击软件推荐
  • 网站绝对路径301关键词爱站网关键词挖掘工具
  • 网站公司网站定制重庆百度快速优化
  • 建网站需要什么语言全国今日新增疫情
  • 怎样做个网站全网
  • 做网站客户改来改去广告营销方式有哪几种
  • 政府网站域名备案优秀网站网页设计分析
  • 什么是b2b网站2023年东莞疫情最新消息
  • dw做网站设计市场调研分析
  • 手游折扣平台最新排名seo在线教程
  • asp做网站用什么写脚本seo黑帽培训
  • 网站建设推广注意什么颜色广告
  • weex做的网站浙江网站建设推广
  • 购物中心设计google 优化推广
  • 宝坻做网站近三天新闻50字左右
  • 电商网站话费充值怎么做搭建网站平台需要多少钱
  • 定制型和模板型网站站长之家alexa排名
  • 腾讯云做网站需要报备江门网站建设
  • php做网站主要怎么布局北京seo邢云涛
  • 专门做化妆品平台的网站有哪些seo比较好的优化方法
  • 销售草皮做网站行吗50篇经典软文100字
  • 岳阳网站设计改版seo网站优化多少钱