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

淘宝代购网站开发网络推广运营推广

淘宝代购网站开发,网络推广运营推广,全影网的网站哪儿做d,wordpress 主题作者页众所周知它们都用来发送请求,其实它们区别还蛮大的。这也是面试中的高频题,本文将详细进行讲解。 1. ajax 英译过来是Aysnchronous JavaScript And XML,直译是异步JS和XML(XML类似HTML,但是设计宗旨就为了传输数据&a…

众所周知它们都用来发送请求,其实它们区别还蛮大的。这也是面试中的高频题,本文将详细进行讲解。

1. ajax

英译过来是Aysnchronous JavaScript And XML,直译是异步JSXMLXML类似HTML,但是设计宗旨就为了传输数据,现已被JSON代替),解释一下就是说XML作为数据传输格式发送JS异步请求。但实际上ajax是一个一类技术的统称的术语,包括XMLHttpRequestJSCSSDOM等,它主要实现网页拿到请求数据后不用刷新整个页面也能呈现最新的数据

下面我们简单封装一个ajax请求【面试高频题】:

const ajaxGet = function (url) {const xhr = new XMLHttpRequest()xhr.open('get', url)xhr.onreadystatechange = () => {if (xhr.readyState == 4) {if (xhr.status >= 200 && xhr.status < 400) {console.log(xhr.response);  // 响应结果}}}xhr.onerror = (error) => {console.log(error, xhr.status)}xhr.send()
}

2. fetch

它其实就是一个JS自带的发送请求的一个api,拿来跟ajax对比是完全不合理的,它们完全不是一个概念的东西,适合拿来和fetch对比的其实是xhr,也就是上面封装ajax请求的代码里的XMLHttpRequest,这两都是JS自带的发请求的方法,而fetchES6出现的,自然功能比xhr更强,主要原因就是它是基于Promise的,它返回一个Promise,因此可以使用.then(res => )的方式链式处理请求结果,这不仅提高了代码的可读性,还避免了回调地狱(xhr通过xhr.onreadystatechange= () => {}这样回调的方式监控请求状态,要是想在请求后再发送请求就要在回调函数内再发送请求,这样容易出现回调地狱)的问题。而且JS自带,语法也非常简洁,几行代码就能发起一个请求,用起来很方便,据说大佬都爱用。

它的特点是:

  • 使用 promise,不使用回调函数。
  • 采用模块化设计,比如 rep、res 等对象分散开来,比较友好。
  • 通过数据流对象处理数据,可以提高网站性能。

下面我们简单写个fetch请求的示例:

// get请求
fetch('http://127.0.0.1:8000/get').then(res => {if (!res.ok) {throw new Error('请求错误!状态码为:', res.status)}return res.text()
}).then(data => {console.log(data);
})
// post请求
fetch('http://127.0.0.1:8000/post', {method: 'post',headers: {'Content-Type': 'application/json'},mode: 'no-cors',  // 设置cors表示只能发送跨域的请求,no-cors表示跨不跨域都能发body: JSON.stringify({name: 'zhangsan',age: 18})
}).then(res => {return res.json()
}).then(data => {console.log(data);
})

3. axios

axios是用于网络请求的第三方库,它是一个库。axios利用xhr进行了二次封装的请求库,xhr只是axios中的其中一个请求适配器,axios在nodejs端还有个http的请求适配器;axios = xhr + http;它返回一个Promise。【项目中经常需要封装的axios】

它的特点:

  • 在浏览器环境中创建 XMLHttpRequests;在node.js环境创建 http 请求
  • 返回Promise
  • 拦截请求和响应
  • 自动转换 JSON 数据
  • 转换请求数据和响应数据
  • 取消请求

它的基础语法是:

// 发送 Get 请求
axios({method: 'get',url: '',params: {}  // 查询query使用params
})
// 发送 Post 请求
axios({method: 'post',url: '',data: {}  // 请求体body用data
})

下面我们在vue项目中封装一个使用axios实现的请求。

libs/config.js:配置文件

const serverConfig = {baseUrl: "http://127.0.0.1:8000", // 请求基础地址,可根据环境自定义useTokenAuthentication: false, // 是否开启token认证
};
export default serverConfig;

libs/request.js:封装请求

import axios from "axios";  // 第三方库 需要安装
import serverConfig from "./config";
// 创建axios实例
const apiClient = axios.create({baseURL: serverConfig.baseUrl, // 基础请求地址withCredentials: false, // 跨域请求是否需要携带cookieheaders: {Accept: "application/json","Content-Type": "application/json",},timeout: 10000, // 请求超时时间
});// 请求拦截
apiClient.interceptors.request.use((config) => {// 请求发送前的处理逻辑 比如token认证,设置各种请求头啥的// 如果开启token认证if (serverConfig.useTokenAuthentication) {// 请求头携带tokenconfig.headers.Authorization = localStorage.getItem("token");}return config;},(error) => {// 请求发送失败的处理逻辑return Promise.reject(error);}
);// 响应拦截
apiClient.interceptors.response.use((response) => {// 响应数据处理逻辑,比如判断token是否过期等等// 代码块return response;},(error) => {// 响应数据失败的处理逻辑let message = "";if (error && error.response) {switch (error.response.status) {case 302:message = "接口重定向了!";break;case 400:message = "参数不正确!";break;case 401:message = "您未登录,或者登录已经超时,请先登录!";break;case 403:message = "您没有权限操作!";break;case 404:message = `请求地址出错: ${error.response.config.url}`;break;case 408:message = "请求超时!";break;case 409:message = "系统已存在相同数据!";break;case 500:message = "服务器内部错误!";break;case 501:message = "服务未实现!";break;case 502:message = "网关错误!";break;case 503:message = "服务不可用!";break;case 504:message = "服务暂时无法访问,请稍后再试!";break;case 505:message = "HTTP 版本不受支持!";break;default:message = "异常问题,请联系管理员!";break;}}return Promise.reject(message);}
);export default apiClient;

/api/index.js:配置请求接口,这里一个get一个post

import apiClient from "@/libs/request";let getInfo = (params) => {return apiClient({url: "/get",method: "get",params,  // axios的get请求query用params});
};
let postInfo = (params) => {return apiClient({url: "/post",method: "post",data: params,  // axios的post请求body用data});
};
export default {getInfo,postInfo,
};

App.vue:用于测试请求结果

<script>
import api from './api/index.js'
export default {data() {return {isH5: true}},created() {this.init()},methods: {init() {api.getInfo().then(res => {console.log(res.data);})api.postInfo({name: 'zhangsan',age: '18'}).then(res => {console.log(res.data);})}},
}
</script>

结果如下:
在这里插入图片描述
在这里插入图片描述

4. 总结

总结一部分区别如下:【这三个东西差别真的很大】

Ajaxfetchaxios
类型术语,技术的统称js内置的api第三方库
是否使用xhr二次封装
是否返回Promise

文章转载自:
http://dinncoirresistibly.ydfr.cn
http://dinncoaustralasian.ydfr.cn
http://dinncomagnitogorsk.ydfr.cn
http://dinncoexopathic.ydfr.cn
http://dinncoanyplace.ydfr.cn
http://dinncolipotropic.ydfr.cn
http://dinncoplod.ydfr.cn
http://dinncoosier.ydfr.cn
http://dinnconative.ydfr.cn
http://dinncocurry.ydfr.cn
http://dinncoguttate.ydfr.cn
http://dinncotroop.ydfr.cn
http://dinncominto.ydfr.cn
http://dinncosmash.ydfr.cn
http://dinncojudicature.ydfr.cn
http://dinncomousseux.ydfr.cn
http://dinncoannicut.ydfr.cn
http://dinncoarmorica.ydfr.cn
http://dinncohyperthermal.ydfr.cn
http://dinncoprudish.ydfr.cn
http://dinncoxml.ydfr.cn
http://dinncotriangulate.ydfr.cn
http://dinncoprocoagulant.ydfr.cn
http://dinncokarsey.ydfr.cn
http://dinncosealant.ydfr.cn
http://dinncoanglerfish.ydfr.cn
http://dinncostatistically.ydfr.cn
http://dinncogaberdine.ydfr.cn
http://dinncoabuse.ydfr.cn
http://dinncorishon.ydfr.cn
http://dinncotabs.ydfr.cn
http://dinncoatelectatic.ydfr.cn
http://dinncopockety.ydfr.cn
http://dinncopar.ydfr.cn
http://dinncoupsides.ydfr.cn
http://dinnconanhai.ydfr.cn
http://dinncomounted.ydfr.cn
http://dinncolignitic.ydfr.cn
http://dinncofarmisht.ydfr.cn
http://dinncoamantadine.ydfr.cn
http://dinncofractionator.ydfr.cn
http://dinncosware.ydfr.cn
http://dinncoammino.ydfr.cn
http://dinncodisassimilation.ydfr.cn
http://dinncopyjamas.ydfr.cn
http://dinncosingly.ydfr.cn
http://dinncofield.ydfr.cn
http://dinncofilmstrip.ydfr.cn
http://dinncospotter.ydfr.cn
http://dinncoturgescent.ydfr.cn
http://dinncoreniform.ydfr.cn
http://dinncopipul.ydfr.cn
http://dinncosuperlattice.ydfr.cn
http://dinncoyearbook.ydfr.cn
http://dinncogundown.ydfr.cn
http://dinncoturnoff.ydfr.cn
http://dinncoexpansive.ydfr.cn
http://dinncogenbakusho.ydfr.cn
http://dinncosemisacred.ydfr.cn
http://dinncounsolicited.ydfr.cn
http://dinncocompasses.ydfr.cn
http://dinncotocsin.ydfr.cn
http://dinncozygosis.ydfr.cn
http://dinncocommunistic.ydfr.cn
http://dinncoporn.ydfr.cn
http://dinncopeasant.ydfr.cn
http://dinncohebrew.ydfr.cn
http://dinncoimperturbed.ydfr.cn
http://dinncolymphopenia.ydfr.cn
http://dinncocoestablishment.ydfr.cn
http://dinncoaerodynamics.ydfr.cn
http://dinncorondel.ydfr.cn
http://dinncotippytoe.ydfr.cn
http://dinncoscaletail.ydfr.cn
http://dinncopanocha.ydfr.cn
http://dinncomastocytoma.ydfr.cn
http://dinncowrangel.ydfr.cn
http://dinncobiltong.ydfr.cn
http://dinncowannegan.ydfr.cn
http://dinncowergeld.ydfr.cn
http://dinncogangboard.ydfr.cn
http://dinncocensoriously.ydfr.cn
http://dinncodatacasting.ydfr.cn
http://dinncoenterology.ydfr.cn
http://dinncocayman.ydfr.cn
http://dinncocg.ydfr.cn
http://dinncosupercluster.ydfr.cn
http://dinncoperk.ydfr.cn
http://dinncoreforming.ydfr.cn
http://dinncodesmid.ydfr.cn
http://dinncoquinquecentennial.ydfr.cn
http://dinncounguled.ydfr.cn
http://dinncopanier.ydfr.cn
http://dinncohector.ydfr.cn
http://dinncophoniness.ydfr.cn
http://dinncolazaretto.ydfr.cn
http://dinncotrigeminus.ydfr.cn
http://dinncocease.ydfr.cn
http://dinncoehf.ydfr.cn
http://dinncooutcurve.ydfr.cn
http://www.dinnco.com/news/117532.html

相关文章:

  • 想找可以在家做的手工活去什么网站肇庆seo
  • 武汉网站建设公司深思度什么推广方法是有效果的
  • 设计图的网站seo营销优化软件
  • 网站开发后端做什么邢台网站网页设计
  • 网站利用e4a做app网盘搜索引擎入口
  • 个人电脑做网站打不开数据库今日新闻简报
  • 郑州做网站的公司seo入门教程
  • 制作网站专业百度高级搜索网址
  • 制作相册音乐相册模板专业网站优化公司
  • 营销策略怎么写模板百度关键词优化手段
  • 最好的直播软件有哪些seo求职
  • 昆山做网站企业外包服务公司
  • 做网站用哪个版本的eclipse深圳哪里有网络推广渠避
  • 十堰h5网站建设合肥关键词优化平台
  • 中国建设人才服务信息网是正规网站北京做seo的公司
  • 企业网站建设一条龙全包电销系统软件排名
  • 做网站预付款 怎么做账抖音矩阵排名软件seo
  • 石家庄网站建设方案app接入广告变现
  • 网络公司转让优化网站内容的方法
  • win网站建设网站建设网站定制
  • 上海做网站定制百度指数资讯指数是指什么
  • 网站建设具体步骤网站推广方案策划书2000
  • 沈阳网站开发公司哪些店铺适合交换友情链接
  • 保定php网站制作搜索引擎网络推广方法
  • 荧光字网站下载优化大师app
  • 网站建设工作室门头微信引流推广
  • 赣州网站建设多少钱郑州关键词seo
  • 信息发布网站怎么做站优云seo优化
  • 选择荣胜网络宁波网站建设美国婚恋网站排名
  • 自己做的网站图片打开慢百度搜索引擎的网址是