Day24 axios基本语法(GET、POST请求)?

大家好我是乌木白!今天要和大家讲 axios 基本语法~

在处理 AJAX 的时候,有一些套件可以使用,我比较常使用的就是 axios , axios 是Vue作者较建议使用在HTTP请求工具上。相较於jQuery来处理 AJAX,axios是更可取的选择。

要引入 axios,可以使用npm下载,也可以直接载入CDN。

axios 基本语法

axios(url [,config])

axios回传的物件是 Promise(fulfilled状态),所以我们可以使用.then.catch处理成功和失败的结果。

const x = axios('url')
x.then((response)=>console.log(x))

axios 默认是GET请求。所以可以写成:

axios('url')
    .then((response)=>console.log(response))
    .catch((error)=>console.log(error));

但实作上比较少看到这样简写,几乎都是 axios.get(...)axios.post(...)这种写法比较多,我们来看看更多方法。

axios 请求方法

我们直接使用axio内建好的请求方法,例如是.get.post:

//GET请求
axios.get('url')
    .then( (response) => console.log(response))
    .catch( (error) => console.log(error))

//POST请求
axios.post('url',{
    email: '[email protected]',
    password: '1234'
})
    .then( (response) => console.log(response))
    .catch( (error) => console.log(error))

还有更多的请求方法,我们可以到 axios 的 Github 查看该如何使用。

总结

axios是常见用来处理AJAX的语法,习惯Vue开发的人,可能用axios会比较多。


<<:  Day18-13. Roman to Integer

>>:  [Day18] 箭头函式

Progressive Web App 定期背景同步 (19)

什麽是 Periodic Background Sync API 透过在 service worke...

JavaScript Day16 - 箭头函式

函式陈述式与函式表达式 函式陈述式:之前直接定义 function 的方式 会被提升到最上面,所以可...

那些注定要没什麽用的专案开发法

(终於可以讲这个了.......吗?) 如果能够不用「是否有成功通过验收并结案」来判段专案是否成功,...

Day9 职训(机器学习与资料分析工程师培训班): python、 php结合highchart

上午: Python程序设计 老师此次课程教学for回圈, List comprehension, ...

[PoEAA] Domain Logic Pattern - Table Module

本篇同步发布於个人Blog: [PoEAA] Domain Logic Pattern - Tabl...