請求接口方法合集

?原生ajax

? ? ? ? get

? ? ? ? let xhr = new XMLHttpRequest()

? ? ? ? xhr.open('get', 'http://www.liulongbin.top:3006/api/getbooks?id=1')

? ? ? ? xhr.send()

? ? ? ? xhr.onreadystatechange = function () {

? ? ? ? ? ? if (this.readyState === 4 && this.status === 200) {

? ? ? ? ? ? ? ? console.log(this.responseText)

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? post

? ? ? ? let xht = new XMLHttpRequest()

? ? ? ? xht.open('post', 'http://47.100.227.25:3000/users/register')

? ? ? ? xht.setRequestHeader('content-type', "application/x-www-form-urlencoded")

? ? ? ? xht.send('username=111&userpwd=111')

? ? ? ? xht.onreadystatechange = function () {

? ? ? ? ? ? if (this.readyState === 4 && this.status === 200) {

? ? ? ? ? ? ? ? console.log(this.responseText)

? ? ? ? ? ? }

? ? ? ? }

jQuery中的ajax

? ? ? ? $.ajax({

? ? ? ? ? ? url: 'http://www.liulongbin.top:3006/api/getbooks?id=1',

? ? ? ? ? ? method: 'GET',

? ? ? ? ? ? // data: 'a=100&b=200',

? ? ? ? ? ? data: { a: 100, b: 200 },

? ? ? ? ? ? dataType: 'json',

? ? ? ? ? ? success(res) {

? ? ? ? ? ? ? ? console.log(res)

? ? ? ? ? ? ? ? console.log(this)

? ? ? ? ? ? },

? ? ? ? ? ? error(xhr, info, err) {

? ? ? ? ? ? },

? ? ? ? ? ? timeout: 1000, // 1s 沒有接收到響應(yīng)就會取消本次請求

? ? ? ? })

? ? ? ? get請求

? ? ? ? $.get('http://www.liulongbin.top:3006/api/getbooks',{id:1},res=>{

? ? ? ? ? ? console.log(res)

? ? ? ? })

? ? ? ?? post請求

? ? ? ? $.post('http://47.100.227.25:3000/users/register','username=111&userpwd=111',res=>{

? ? ? ? ? ? console.log(res)

? ? ? ? })

?promise-ajax

? ? ? ? new Promise((resolve, reject) => {

? ? ? ? ? ? $.get('http://www.liulongbin.top:3006/api/getbooks?id=1',res=>{

? ? ? ? ? ? ? ? resolve(res)

? ? ? ? ? ? })

? ? ? ? }).then(res=>{

? ? ? ? ? ? console.log(res)

? ? ? ? })

? ? axios

? ? ? ? ?? get

? ? ? ? axios.get('http://www.liulongbin.top:3006/api/getbooks',{

? ? ? ? ? ? params:{

? ? ? ? ? ? ? ? id:1

? ? ? ? ? ? }

? ? ? ? })

? ? ? ? .then(res=>{

? ? ? ? ? ? console.log(res)

? ? ? ? })

? ? ? ? post

? ? ? ? axios.post('http://47.100.227.25:3000/users/register','username=111&userpwd=111',{

? ? ? ? ? ? username:111,

? ? ? ? ? ? userpwd:111

? ? ? ? })

? ? ? ? .then(res=>{

? ? ? ? ? ? console.log(res)

? ? ? ? })

async - axios

? ? ? ? async function fn(){

? ? ? ? ? ? ?let res = await axios.get('http://www.liulongbin.top:3006/api/getbooks?id=1')

? ? ? ? ? ? ?console.log(res)

? ? ? ? }

? ? ? ? fn()

```

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容