ajax在js中和jQuery中的用法實例詳解
原生 JS
怎么發(fā)送一個 get 請求
- 創(chuàng)建一個 ajax 對象
- var xhr = new XMLHttpRequest()
- 設(shè)置請求方式和請求地址[,是否異步]
- xhr.open('get', '/ajax'[, true or fasle])
- 準(zhǔn)備接受請求體
- xhr.onload = function () { console.log(xhr.responseText) }
- xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log( xhr.responseText ) } }
- 發(fā)送請求
- xhr.send(null)
- xhr.send(null)
var xhr = new XMLHttpRequest() xhr.open('get', '/ajax') xhr.onload = function () { console.log(xhr.responseText) } xhr.send(null)
怎么發(fā)送一個 post 請求
- 創(chuàng)建一個 ajax 對象
- var xhr = new XMLHttpRequest()
- 設(shè)置請求方式和請求地址[,是否異步]
- xhr.open('post', '/ajax'[, true or fasle])
- 準(zhǔn)備接受請求體
- xhr.onload = function () { console.log(xhr.responseText) }
- xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log( xhr.responseText ) } }
- 發(fā)送請求
- xhr.send(null)
var xhr = new XMLHttpRequest() xhr.open('post', '/ajax') xhr.onload = function () { console.log(xhr.responseText) } xhr.send(null)
發(fā)送一個帶有參數(shù)的 get 請求
- var xhr = new XMLHttpRequest
- 直接在請求地址后面拼接參數(shù),? 開始,key=value 的形式,多個參數(shù)之間以 & 分割
- xhr.open('get', '/ajax?name=Jack&age=18')
- xhr.onload = function () { console.log( xhr.responseText ) }
- xhr.send()
發(fā)送一個帶有參數(shù)的 post 請求
var xhr = new XMLHttpRequest
不需要在請求地址后面拼接任何內(nèi)容
- xhr.open('post', '/ajax')
xhr.onload = function () { console.log( xhr.responseText ) }
post 方式攜帶參數(shù)是直接寫在 xhr.send() 后面的 () 里面
- 自己收集數(shù)據(jù) key=value
- 自己設(shè)置請求頭
- xhr.setRequestHeadr('content-type', 'application/x-www-form-urlencoded')
- FormData 收集數(shù)據(jù)
- 什么都不需要,只要使用 FormData 收集數(shù)據(jù)就可以了
- var fd = new FormData(DOM)
- 在發(fā)送請求的時候只要把 fd 帶過去就行了
var xhr = new XMLHttpRequest() xhr.open('post', '/ajax') xhr.onload = function () { console.log(xhr.responseText) } xhr.setRequestHeadr('content-type', 'application/x-www-form-urlencoded') xhr.send('key=value&key=value')
var fd = new FormData(document.querySelector('form')) var xhr = new XMLHttpRequest() xhr.open('post', '/ajax') xhr.onload = function () { console.log(xhr.responseText) } xhr.send(fd)
jQuery
$.get 幾個參數(shù),怎么使用
地址
- 參數(shù) key=value 或者 { name: 'Jack' }
- 成功的回調(diào)函數(shù)
- 預(yù)期后臺返回的數(shù)據(jù)類型
- text : 什么都不做,直接給你結(jié)果
- json : 必定會執(zhí)行一步 JSON.parse()
$.post 幾個參數(shù),怎么使用
- 地址
- 參數(shù) key=value 或者 { name: 'Jack' }, 不能發(fā)送 FormData
- 成功的回調(diào)函數(shù)
- 預(yù)期后臺返回的數(shù)據(jù)類型
$.ajax 幾個參數(shù),怎么使用
- 就是配置項 options
- url: 請求地址
- method/type: 請求方式
- data: 攜帶參數(shù)
- dataType: 后臺返回的數(shù)據(jù)類型天
- success: 成功的回掉
- error: 失敗的回調(diào)
- contentType: 發(fā)送 FormData 的時候使用的
- processData: 發(fā)送 FormData 的時候使用的
JSONP
$.ajax 怎么發(fā)送 jaonp 請求
- dataType 必須是 jsonp
- 方式必須是 get
- jsonp: 根據(jù)后臺來決定
$.ajax({ url: '/jsonp', data: {}, dataType: 'jsonp', jsonp: 'callback', success (res) { console.log(res) } })
總結(jié)
到此這篇關(guān)于ajax在js中和jQuery中的用法的文章就介紹到這了,更多相關(guān)ajax在js中和jQuery的用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript對象拷貝與Object.assign用法實例分析
這篇文章主要介紹了JavaScript對象拷貝與Object.assign用法,結(jié)合實例形式分析了javascript深拷貝與淺拷貝以及Object.assign的功能與相關(guān)使用技巧,需要的朋友可以參考下2018-06-06詳解JS中的this、apply、call、bind(經(jīng)典面試題)
JS中的this、apply、call、bind是一道經(jīng)典面試題,最好還是了解一下 this 的指向和 call、apply、bind 三者的區(qū)別。下面就跟隨腳本之家小編一起學(xué)習(xí)this、apply、call、bind的知識吧2017-09-09微信小程序 JS動態(tài)修改樣式的實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于微信小程序JS動態(tài)修改樣式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12element?UI中在?el-select?與?el-tree?結(jié)合組件實現(xiàn)過程
項目上實現(xiàn)某個功能,使用到了?el-select?和?el-tree?組合實現(xiàn),記錄下兩者結(jié)合的實現(xiàn)過程,對?el-select?與?el-tree?結(jié)合組件實現(xiàn)過程感興趣的朋友跟隨小編一起看看吧2023-02-02ES6新特性之解構(gòu)、參數(shù)、模塊和記號用法示例
這篇文章主要介紹了ES6新特性之解構(gòu)、參數(shù)、模塊和記號用法,結(jié)合實例形式分析了解構(gòu)、參數(shù)、模塊和記號的功能、用法及相關(guān)使用注意事項,需要的朋友可以參考下2017-04-04