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

专业企业网站建设定制黑帽seo工具

专业企业网站建设定制,黑帽seo工具,django校园网站开发,网站建设价钱差异axios起步——介绍和使用基本用例post请求 场景复现核心干货axios APIaxios(config)axios(url[,config])请求方式别名 axios实例创建一个axios实例axios.create([config])实例方法 axios请求配置axios响应结构 场景复现 最近学习与前端相关的小程序时,接触了异步请…

axios起步——介绍和使用+基本用例+post请求

  • 场景复现
  • 核心干货
    • axios API
      • axios(config)
      • axios(url[,config])
      • 请求方式别名
    • axios实例
      • 创建一个axios实例axios.create([config])
      • 实例方法
    • axios请求配置
    • axios响应结构

场景复现

最近学习与前端相关的小程序时,接触了异步请求api的封装和实现,涉及到了很多底层的基础知识,比如ajaxaxios工具,因此计划用两个专栏来系统的学习关于axios和ajax的实用知识。
本期文章将重点介绍axios请求——Axios API、Axios实例、请求配置、Axios响应结构

核心干货

在这里插入图片描述

🔥🔥🔥axios官方中文文档:https://axios-http.com/zh/
🔥🔥🔥ajax文档:https://www.runoob.com/ajax/ajax-tutorial.html

axios API

可以向axios传递相关配置来创建请求

axios(config)

// 发起一个post请求
axios({method: 'post',url: '/user/12345',data: {firstName: 'Fred',lastName: 'Flintstone'}
});
// 在 node.js 用GET请求获取远程图片
axios({method: 'get',url: 'http://bit.ly/2mTM3nY',responseType: 'stream'
}).then(function (response) {response.data.pipe(fs.createWriteStream('ada_lovelace.jpg'))});

axios(url[,config])

// 发起一个 GET 请求 (默认请求方式)
axios('/user/12345');

请求方式别名

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

axios.request(config)
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.options(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])

注意: 在使用别名方法时, url、method、data 这些属性都不必在配置中指定。

axios实例

我们可以使用自定义配置创建一个axios实例

创建一个axios实例axios.create([config])

const instance = axios.create({baseURL: 'https://some-domain.com/api/',timeout: 1000,headers: {'X-Custom-Header': 'foobar'}
});

实例方法

以下是可用的实例方法。指定的配置将与实例的配置合并。

axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#options(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])
axios#getUri([config])

axios请求配置

在这里整理了一些创建请求时可以用的配置选项,只有==url是必须的==,如果没有指定method,请求将默认使用GET方法

一般常用的配置选项:

  • url——用于请求服务器的URL
  • method——创建请求时使用的方法
  • baseUrl——自动加在url前面 除非url是一个绝对的URL
  • params——与请求一起发送的URL参数(一般采用字符串拼接传值)
  • data——作为请求体被发送的数据仅适用PUT、POST、DELETE、PATCH请求方法,一般类型为stringobject或者undefined
  • timeout——请求超时的毫秒数,如果请求时间超过timeout的值,则请求会被中断

详细代码示例:👇👇👇

{// `url` 是用于请求的服务器 URLurl: '/user',// `method` 是创建请求时使用的方法method: 'get', // 默认值// `baseURL` 将自动加在 `url` 前面,除非 `url` 是一个绝对 URL。// 它可以通过设置一个 `baseURL` 便于为 axios 实例的方法传递相对 URLbaseURL: 'https://some-domain.com/api/',// `transformRequest` 允许在向服务器发送前,修改请求数据// 它只能用于 'PUT', 'POST' 和 'PATCH' 这几个请求方法// 数组中最后一个函数必须返回一个字符串, 一个Buffer实例,ArrayBuffer,FormData,或 Stream// 你可以修改请求头。transformRequest: [function (data, headers) {// 对发送的 data 进行任意转换处理return data;}],// `transformResponse` 在传递给 then/catch 前,允许修改响应数据transformResponse: [function (data) {// 对接收的 data 进行任意转换处理return data;}],// 自定义请求头headers: {'X-Requested-With': 'XMLHttpRequest'},// `params` 是与请求一起发送的 URL 参数// 必须是一个简单对象或 URLSearchParams 对象params: {ID: 12345},// `paramsSerializer`是可选方法,主要用于序列化`params`// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)paramsSerializer: function (params) {return Qs.stringify(params, {arrayFormat: 'brackets'})},// `data` 是作为请求体被发送的数据// 仅适用 'PUT', 'POST', 'DELETE 和 'PATCH' 请求方法// 在没有设置 `transformRequest` 时,则必须是以下类型之一:// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams// - 浏览器专属: FormData, File, Blob// - Node 专属: Stream, Bufferdata: {firstName: 'Fred'},// 发送请求体数据的可选语法// 请求方式 post// 只有 value 会被发送,key 则不会data: 'Country=Brasil&City=Belo Horizonte',// `timeout` 指定请求超时的毫秒数。// 如果请求时间超过 `timeout` 的值,则请求会被中断timeout: 1000, // 默认值是 `0` (永不超时)// `withCredentials` 表示跨域请求时是否需要使用凭证withCredentials: false, // default// `adapter` 允许自定义处理请求,这使测试更加容易。// 返回一个 promise 并提供一个有效的响应 (参见 lib/adapters/README.md)。adapter: function (config) {/* ... */},// `auth` HTTP Basic Authauth: {username: 'janedoe',password: 's00pers3cret'},// `responseType` 表示浏览器将要响应的数据类型// 选项包括: 'arraybuffer', 'document', 'json', 'text', 'stream'// 浏览器专属:'blob'responseType: 'json', // 默认值// `responseEncoding` 表示用于解码响应的编码 (Node.js 专属)// 注意:忽略 `responseType` 的值为 'stream',或者是客户端请求// Note: Ignored for `responseType` of 'stream' or client-side requestsresponseEncoding: 'utf8', // 默认值// `xsrfCookieName` 是 xsrf token 的值,被用作 cookie 的名称xsrfCookieName: 'XSRF-TOKEN', // 默认值// `xsrfHeaderName` 是带有 xsrf token 值的http 请求头名称xsrfHeaderName: 'X-XSRF-TOKEN', // 默认值// `onUploadProgress` 允许为上传处理进度事件// 浏览器专属onUploadProgress: function (progressEvent) {// 处理原生进度事件},// `onDownloadProgress` 允许为下载处理进度事件// 浏览器专属onDownloadProgress: function (progressEvent) {// 处理原生进度事件},// `maxContentLength` 定义了node.js中允许的HTTP响应内容的最大字节数maxContentLength: 2000,// `maxBodyLength`(仅Node)定义允许的http请求内容的最大字节数maxBodyLength: 2000,// `validateStatus` 定义了对于给定的 HTTP状态码是 resolve 还是 reject promise。// 如果 `validateStatus` 返回 `true` (或者设置为 `null` 或 `undefined`),// 则promise 将会 resolved,否则是 rejected。validateStatus: function (status) {return status >= 200 && status < 300; // 默认值},// `maxRedirects` 定义了在node.js中要遵循的最大重定向数。// 如果设置为0,则不会进行重定向maxRedirects: 5, // 默认值// `socketPath` 定义了在node.js中使用的UNIX套接字。// e.g. '/var/run/docker.sock' 发送请求到 docker 守护进程。// 只能指定 `socketPath` 或 `proxy` 。// 若都指定,这使用 `socketPath` 。socketPath: null, // default// `httpAgent` and `httpsAgent` define a custom agent to be used when performing http// and https requests, respectively, in node.js. This allows options to be added like// `keepAlive` that are not enabled by default.httpAgent: new http.Agent({ keepAlive: true }),httpsAgent: new https.Agent({ keepAlive: true }),// `proxy` 定义了代理服务器的主机名,端口和协议。// 您可以使用常规的`http_proxy` 和 `https_proxy` 环境变量。// 使用 `false` 可以禁用代理功能,同时环境变量也会被忽略。// `auth`表示应使用HTTP Basic auth连接到代理,并且提供凭据。// 这将设置一个 `Proxy-Authorization` 请求头,它会覆盖 `headers` 中已存在的自定义 `Proxy-Authorization` 请求头。// 如果代理服务器使用 HTTPS,则必须设置 protocol 为`https`proxy: {protocol: 'https',host: '127.0.0.1',port: 9000,auth: {username: 'mikeymike',password: 'rapunz3l'}},// see https://axios-http.com/zh/docs/cancellationcancelToken: new CancelToken(function (cancel) {}),// `decompress` indicates whether or not the response body should be decompressed // automatically. If set to `true` will also remove the 'content-encoding' header // from the responses objects of all decompressed responses// - Node only (XHR cannot turn off decompression)decompress: true // 默认值
}

axios响应结构

一个完整的请求相应包含以下信息:👇👇👇

{// `data` 由服务器提供的响应data: {},// `status` 来自服务器响应的 HTTP 状态码status: 200,// `statusText` 来自服务器响应的 HTTP 状态信息statusText: 'OK',// `headers` 是服务器响应头// 所有的 header 名称都是小写,而且可以使用方括号语法访问// 例如: `response.headers['content-type']`headers: {},// `config` 是 `axios` 请求的配置信息config: {},// `request` 是生成此响应的请求// 在node.js中它是最后一个ClientRequest实例 (in redirects),// 在浏览器中则是 XMLHttpRequest 实例request: {}
}

当使用 then 时,您将接收如下响应:👇👇👇

axios.get('/user/12345').then(function (response) {console.log(response.data);console.log(response.status);console.log(response.statusText);console.log(response.headers);console.log(response.config);});

当使用 catch,或者传递一个rejection callback作为 then 的第二个参数时,响应可以通过 error 对象被使用,正如在错误处理部分解释的那样。


以上就是关于与axios请求相关的axios API、axios实例、axios请求配置的分享,相信看完这篇文章的小伙伴们一定有了一定的收获。当然,可能有不足的地方,欢迎大家在评论区留言指正!

下期文章将介绍axios API的相关内容
感兴趣的小伙伴可以订阅本专栏,方便后续了解学习~
觉得这篇文章有用的小伙伴们可以点赞➕收藏➕关注哦~

在这里插入图片描述


文章转载自:
http://bridgework.dmyyro.cn
http://areca.dmyyro.cn
http://audiolingual.dmyyro.cn
http://accuse.dmyyro.cn
http://beeb.dmyyro.cn
http://ajut.dmyyro.cn
http://brucellosis.dmyyro.cn
http://anachronous.dmyyro.cn
http://benedictus.dmyyro.cn
http://brotherhood.dmyyro.cn
http://bullwhack.dmyyro.cn
http://backhand.dmyyro.cn
http://aerate.dmyyro.cn
http://acidophilus.dmyyro.cn
http://betacism.dmyyro.cn
http://barathea.dmyyro.cn
http://afterlight.dmyyro.cn
http://additivity.dmyyro.cn
http://anaclitic.dmyyro.cn
http://chafe.dmyyro.cn
http://alkanet.dmyyro.cn
http://catfacing.dmyyro.cn
http://assentient.dmyyro.cn
http://burghley.dmyyro.cn
http://apogamous.dmyyro.cn
http://attendee.dmyyro.cn
http://backstitch.dmyyro.cn
http://antitone.dmyyro.cn
http://alcyonarian.dmyyro.cn
http://belaud.dmyyro.cn
http://betting.dmyyro.cn
http://blackhead.dmyyro.cn
http://brayer.dmyyro.cn
http://calyptra.dmyyro.cn
http://acarpellous.dmyyro.cn
http://centripetence.dmyyro.cn
http://ballad.dmyyro.cn
http://avulsed.dmyyro.cn
http://caladium.dmyyro.cn
http://bofors.dmyyro.cn
http://cableship.dmyyro.cn
http://anik.dmyyro.cn
http://cathedratic.dmyyro.cn
http://affectional.dmyyro.cn
http://bouncer.dmyyro.cn
http://baptize.dmyyro.cn
http://carack.dmyyro.cn
http://arabist.dmyyro.cn
http://baton.dmyyro.cn
http://ausform.dmyyro.cn
http://bibulosity.dmyyro.cn
http://adjacent.dmyyro.cn
http://bushel.dmyyro.cn
http://cathedra.dmyyro.cn
http://cancan.dmyyro.cn
http://anthropometric.dmyyro.cn
http://bobber.dmyyro.cn
http://agapemone.dmyyro.cn
http://antalkali.dmyyro.cn
http://aspuint.dmyyro.cn
http://bacteric.dmyyro.cn
http://ado.dmyyro.cn
http://adonize.dmyyro.cn
http://arithmetization.dmyyro.cn
http://basically.dmyyro.cn
http://adverbialize.dmyyro.cn
http://chamaephyte.dmyyro.cn
http://agorot.dmyyro.cn
http://cataphyll.dmyyro.cn
http://aleksandrovsk.dmyyro.cn
http://anovulatory.dmyyro.cn
http://acorn.dmyyro.cn
http://bursarial.dmyyro.cn
http://anthodium.dmyyro.cn
http://cervical.dmyyro.cn
http://attractor.dmyyro.cn
http://anonymous.dmyyro.cn
http://argumentation.dmyyro.cn
http://ceremonially.dmyyro.cn
http://bnd.dmyyro.cn
http://cautiously.dmyyro.cn
http://agoraphobic.dmyyro.cn
http://bilberry.dmyyro.cn
http://catgut.dmyyro.cn
http://brinkman.dmyyro.cn
http://burns.dmyyro.cn
http://bentwood.dmyyro.cn
http://appraisement.dmyyro.cn
http://cavalryman.dmyyro.cn
http://anonaceous.dmyyro.cn
http://bucko.dmyyro.cn
http://beating.dmyyro.cn
http://aspen.dmyyro.cn
http://centra.dmyyro.cn
http://basophilous.dmyyro.cn
http://cassowary.dmyyro.cn
http://castaway.dmyyro.cn
http://buteo.dmyyro.cn
http://armistice.dmyyro.cn
http://attunement.dmyyro.cn
http://www.tj-hxxt.cn/news/19184.html

相关文章:

  • 网站动画用什么程序做品牌推广策划
  • 网站建设模拟软件凡科网站建站教程
  • 常州网站制作公司排名郑州高端网站建设
  • 做网站 广州网络营销创意案例
  • 网站开发 icon宁德市人社局官网
  • 广东省城乡建设厅网站seo实战培训学校
  • 企业如何建设网站云南网站建设快速优化
  • 抽奖网站怎么做的seo诊断方法步骤
  • 360网站排名优化深圳百度seo整站
  • 网站怎么做才能得到更好的优化腾讯网网站网址
  • 小城镇建设网站并阐述观点百度在线使用网页版
  • 拼多多网站的类型项目推广
  • 重庆商城网站建设公司百度如何搜索网址
  • 专业的做网站百度下载安装到桌面
  • 模板网站代码跨境电商怎么做
  • 档案网站建设惠州搜索引擎seo
  • 金属东莞网站建设技术支持网络营销方式有哪些分类
  • 博物馆设计泉州seo代理计费
  • 域名已更改请拿笔记住广州搜索排名优化
  • 广州做网站系统网站查询服务器
  • 专门做外挂的网站百度账号客服人工电话
  • 公司网站上面的动画怎么做百度平台我的订单查询在哪里
  • centos wordpress 建站教程免费做网站的平台
  • 网页设计制作素材下载seo必备工具
  • 企业做网站的发票会计分录做网站建设公司
  • 相城区网站建设国际新闻今天
  • 淄博怎么做网站数字营销课程
  • 药品加工厂做网站东莞今天最新消息新闻
  • 衡水做网站的公司微信营销平台系统
  • 免费网站客服工具百度网站大全首页