ajax在js中和jQuery中的用法實(shí)例詳解
原生 JS
怎么發(fā)送一個(gè) get 請(qǐng)求
- 創(chuàng)建一個(gè) ajax 對(duì)象
- var xhr = new XMLHttpRequest()
- 設(shè)置請(qǐng)求方式和請(qǐng)求地址[,是否異步]
- xhr.open('get', '/ajax'[, true or fasle])
- 準(zhǔn)備接受請(qǐng)求體
- xhr.onload = function () { console.log(xhr.responseText) }
- xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log( xhr.responseText ) } }
- 發(fā)送請(qǐng)求
- 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ā)送一個(gè) post 請(qǐng)求
- 創(chuàng)建一個(gè) ajax 對(duì)象
- var xhr = new XMLHttpRequest()
- 設(shè)置請(qǐng)求方式和請(qǐng)求地址[,是否異步]
- xhr.open('post', '/ajax'[, true or fasle])
- 準(zhǔn)備接受請(qǐng)求體
- xhr.onload = function () { console.log(xhr.responseText) }
- xhr.onreadystatechange = function () { if (xhr.readyState === 4) { console.log( xhr.responseText ) } }
- 發(fā)送請(qǐng)求
- xhr.send(null)
var xhr = new XMLHttpRequest() xhr.open('post', '/ajax') xhr.onload = function () { console.log(xhr.responseText) } xhr.send(null)
發(fā)送一個(gè)帶有參數(shù)的 get 請(qǐng)求
- var xhr = new XMLHttpRequest
- 直接在請(qǐng)求地址后面拼接參數(shù),? 開始,key=value 的形式,多個(gè)參數(shù)之間以 & 分割
- xhr.open('get', '/ajax?name=Jack&age=18')
- xhr.onload = function () { console.log( xhr.responseText ) }
- xhr.send()
發(fā)送一個(gè)帶有參數(shù)的 post 請(qǐng)求
var xhr = new XMLHttpRequest
不需要在請(qǐng)求地址后面拼接任何內(nèi)容
- xhr.open('post', '/ajax')
xhr.onload = function () { console.log( xhr.responseText ) }
post 方式攜帶參數(shù)是直接寫在 xhr.send() 后面的 () 里面
- 自己收集數(shù)據(jù) key=value
- 自己設(shè)置請(qǐng)求頭
- xhr.setRequestHeadr('content-type', 'application/x-www-form-urlencoded')
- FormData 收集數(shù)據(jù)
- 什么都不需要,只要使用 FormData 收集數(shù)據(jù)就可以了
- var fd = new FormData(DOM)
- 在發(fā)送請(qǐng)求的時(shí)候只要把 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 幾個(gè)參數(shù),怎么使用
地址
- 參數(shù) key=value 或者 { name: 'Jack' }
- 成功的回調(diào)函數(shù)
- 預(yù)期后臺(tái)返回的數(shù)據(jù)類型
- text : 什么都不做,直接給你結(jié)果
- json : 必定會(huì)執(zhí)行一步 JSON.parse()
$.post 幾個(gè)參數(shù),怎么使用
- 地址
- 參數(shù) key=value 或者 { name: 'Jack' }, 不能發(fā)送 FormData
- 成功的回調(diào)函數(shù)
- 預(yù)期后臺(tái)返回的數(shù)據(jù)類型
$.ajax 幾個(gè)參數(shù),怎么使用
- 就是配置項(xiàng) options
- url: 請(qǐng)求地址
- method/type: 請(qǐng)求方式
- data: 攜帶參數(shù)
- dataType: 后臺(tái)返回的數(shù)據(jù)類型天
- success: 成功的回掉
- error: 失敗的回調(diào)
- contentType: 發(fā)送 FormData 的時(shí)候使用的
- processData: 發(fā)送 FormData 的時(shí)候使用的
JSONP
$.ajax 怎么發(fā)送 jaonp 請(qǐng)求
- dataType 必須是 jsonp
- 方式必須是 get
- jsonp: 根據(jù)后臺(tái)來決定
$.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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- jquery.jsPlumb實(shí)現(xiàn)拓?fù)鋱D
- JavaScript/jQuery實(shí)現(xiàn)切換頁面效果
- JavaScript實(shí)現(xiàn)樓梯滾動(dòng)特效(jQuery實(shí)現(xiàn))
- js實(shí)現(xiàn)七夕表白彈幕效果 jQuery實(shí)現(xiàn)彈幕技術(shù)
- JavaScript與JQuery框架基礎(chǔ)入門教程
- JS實(shí)現(xiàn)jQuery的append功能
- jQuery是用來干什么的 jquery其實(shí)就是一個(gè)js框架
- JavaScript中通用的jquery動(dòng)畫滾屏實(shí)例
相關(guān)文章
JavaScript對(duì)象拷貝與Object.assign用法實(shí)例分析
這篇文章主要介紹了JavaScript對(duì)象拷貝與Object.assign用法,結(jié)合實(shí)例形式分析了javascript深拷貝與淺拷貝以及Object.assign的功能與相關(guān)使用技巧,需要的朋友可以參考下2018-06-06javascript實(shí)現(xiàn)QQ空間相冊(cè)展示源碼
本文給大家分享基于javascript制作的qq空間相冊(cè)展示效果,涉及到html\css布局思維,浮動(dòng)定位詳解,具體實(shí)現(xiàn)代碼大家參考下本文2017-12-12詳解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的知識(shí)吧2017-09-09微信小程序 JS動(dòng)態(tài)修改樣式的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于微信小程序JS動(dòng)態(tài)修改樣式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12element?UI中在?el-select?與?el-tree?結(jié)合組件實(shí)現(xiàn)過程
項(xiàng)目上實(shí)現(xiàn)某個(gè)功能,使用到了?el-select?和?el-tree?組合實(shí)現(xiàn),記錄下兩者結(jié)合的實(shí)現(xiàn)過程,對(duì)?el-select?與?el-tree?結(jié)合組件實(shí)現(xiàn)過程感興趣的朋友跟隨小編一起看看吧2023-02-02微信小程序遍歷Echarts圖表實(shí)現(xiàn)多個(gè)餅圖
這篇文章主要介紹了微信小程序遍歷Echarts圖表實(shí)現(xiàn)多個(gè)餅圖,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04ES6新特性之解構(gòu)、參數(shù)、模塊和記號(hào)用法示例
這篇文章主要介紹了ES6新特性之解構(gòu)、參數(shù)、模塊和記號(hào)用法,結(jié)合實(shí)例形式分析了解構(gòu)、參數(shù)、模塊和記號(hào)的功能、用法及相關(guān)使用注意事項(xiàng),需要的朋友可以參考下2017-04-04