2023-03-22
xhr ○
ajax 常見請求方式有哪些
● GET
○表示向服務器獲取資源
●POST
○表示向服務器提交信息,通常用于產生新的數據,比如注冊
●PUT
○表示希望修改服務器的數據, 通常用于修改某數據
●DELETE
○表示希望刪除服務器的數據
●OPTIONS
○發生在跨域的預檢請求中,表示客戶端向服務器申請跨域提交
ajax 中 get 和 post 請求攜帶參數的方式
●GET: 直接拼接在請求路徑后, 以 ? 間隔, 使用 key=value 的形式書寫, 當有多個參數的時候用 & 連接
const xhr = new XMLHttpRequest()
xhr.open('GET', 'http://localhost:8888/test/third?name=QF666&age=18')
xhr.send()
xhr.onload = function () {
let res = JSON.parse(xhr.responseText)
console.log(res)
}
●POST
○在請求體內攜帶參數(其實就是 send 小括號內部)
○并且需要設置請求頭內部的 content-type
■如果參數為 查詢字符串, 需要添加:
●'xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')'
■如果參數為 JSON 字符串, 需要添加:
●'xhr.setRequestHeader('content-type', 'application/json')'
const xhr = new XMLHttpRequest()
xhr.open('POST', 'http://localhost:8888/test/fourth')
xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded')
xhr.send('name=QF666&age=18')
xhr.onload = function () {
let res = JSON.parse(xhr.responseText)
console.log(res)
}
開班時間:2021-04-12(深圳)
開班盛況開班時間:2021-05-17(北京)
開班盛況開班時間:2021-03-22(杭州)
開班盛況開班時間:2021-04-26(北京)
開班盛況開班時間:2021-05-10(北京)
開班盛況開班時間:2021-02-22(北京)
開班盛況開班時間:2021-07-12(北京)
預約報名開班時間:2020-09-21(上海)
開班盛況開班時間:2021-07-12(北京)
預約報名開班時間:2019-07-22(北京)
開班盛況
Copyright 2011-2023 北京千鋒互聯科技有限公司 .All Right
京ICP備12003911號-5
京公網安備 11010802035720號