let xhr = new XMLHttpRequest(); //建立XMLHttpRequest對象實例
xhr.open(); //與服務端創建鏈接
xhr.send(); //發送請求
xhr.onreadystatechange //處理響應
複製代碼
注:IE中有三種不一樣的XHR對象,咱們這裏只談最經常使用的,適用於IE7及以上的前端
前端頁面代碼ajax
let xhr = new XMLHttpRequest();
xhr.open('get','URL?value=string');//將拼串的URL放置此
xhr.send();
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
//接受響應的操做
}
}
複製代碼
readyState表明XHR對象所處於的狀態,他有5個值express
XHR對象接收到所有響應後,數據會填滿對象,此時須要調用onreadystatechange方法來監控XHR所處於的狀態,由於每當readyState數值發生變化,都會調用一次onreadystatechange方法後端
咱們通常在readyState = 4狀態時操做響應數據,而且咱們會監控服務端返回的狀態碼瀏覽器
當以2開頭時,服務器返回的響應即爲正確,除此以外,304的意思爲頁面正確且會重定向到瀏覽器已有的緩存文件,因此咱們能夠將接收操做處改成緩存
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304)){
//接受響應的操做
}
}
複製代碼
Node服務器代碼bash
app.get('/',(req,res)=>{
console.log(req.query);//get請求使用query參數接收
res.send('ok')
});
複製代碼
前端頁面代碼服務器
let xhr = new XMLHttpRequest();
xhr.open('post','http://localhost:8088');//不須要拼串了
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');//設置請求頭
xhr.send('key=value'); //發送數據(請求體)
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.staus === 200){
//接受響應的操做
}
}
複製代碼
與前端頁面同樣,後端post也與get請求略微不一樣app
Node服務器代碼異步
app.use(express.urlencoded({extended:true}))//我使用的是express內置的中間件
app.post('/',(req,res)=>{
let {key} = req.body; //咱們能夠直接使用解構賦值來獲取body中相應的值
console.log({key});
res.send('ok');
})
複製代碼
標準寫法:
$.ajax('請求的地址',{
method:'', //請求的方法
data:{}, //請求的數據
dataType:'', //響應的數據類型
success:(result)=>{}, //成功回調
error:(err)=>{} //失敗回調
})
簡寫:
$.get/post('請求地址',data,(data(響應的數據),success,xhr)=>{},dataType)
複製代碼
前端頁面代碼
let sendAjax = function (method,url,data){
return new Promise(()=>{
let xhr = new XMLHttpRequest();
//判斷是’get‘請求仍是’post‘請求
if(method.toLowerCase() === 'get'){
url += ('?' + data); //get請求須要進行拼串操做
xhr.open(method,url);
xhr.send();
}else if(method.toLowerCase() === 'post'){
xhr.open(method,url);
xhr.setRequestHeader('content-Type','application/x-www-form-urlencoded')
xhr.send(data);
}
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && ((xhr.status >= 200 && xhr.status < 300) || xhr.status === 304)){
//接受響應的操做
resolve(); //返回成功
}else{
reject(); //返回失敗
}
}
})
}
function clickB(){
sendAjax('post','http://localhost:8088','a=1')
}
複製代碼
let keys = Object.keys(data); //獲取全部的key值
if(data instanceof Object){ //判斷是否爲對象,若是不是能夠將其轉化爲對象
data = keys.reduce((pre,now)=>{ //使用reduce方法累計附加對象中的key和value
return pre += `${now}=${data[now]}&`;//使用模板字符串來簡化寫法
},'')
}
複製代碼
let value = {
name:'sys',
age:18
}
let sendAjax = function (method,url,data){
return new Promise(()=>{
let xhr = new XMLHttpRequest();
let keys = Object.keys(data);
if(data instanceof Object){
data = keys.reduce((pre,now)=>{
return pre += `${now}=${data[now]}&`;
},'')
}
if(method.toLowerCase() === 'get'){
url += ('?'+ data);
xhr.open(method,url);
xhr.send();
}else
if(method.toLowerCase() === 'post'){
xhr.open(method,url);
xhr.setRequestHeader('content-Type','application/x-www-form-urlencoded')
xhr.send(data);
}
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.staus === 200){
//接受響應的操做
}
}
})
}
function clickB(){
sendAjax('get','http://localhost:8088',value)
}
複製代碼
借鑑:《JavaScript高級程序設計》 + 網上查找 + 本身的理解
若有錯誤,但願提出
但願你們都能早日拿到心儀的offer,加油,共勉