国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

利用promise及參數(shù)解構(gòu)封裝ajax請求的方法

瀏覽:210日期:2022-06-11 15:39:48
目錄
  • 1.前端代碼
  • 2.后端代碼
  • 3.注:

1.前端代碼

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <script> /**  * type: get/post  * url: http://localhost:3000 http://localhost:3000/details http://localhost:3000/users  * data: lid=5 / uname=lili&upwd=123456  * dataType: "" / "json", 如果服務(wù)端返回的是json格式字符串,就通過dataType通知ajax函數(shù)自動轉(zhuǎn)換為對象  * **/ ajax({  type: "get",  url: "http://localhost:3000",  dataType: "json" }) // data 不寫在解構(gòu)時(shí)值默認(rèn)為 data: undefined ajax({  type: "get",  url: "http://localhost:3000/details",  data: "lid=0",  dataType: "json" }) ajax({  type: "post",   url: "http://localhost:3000/users",   data: "uname=lili&upwd=123456", }).then(function(res){  alert(res) }) // dataType 不寫在解構(gòu)時(shí)值默認(rèn)為 dataType: undefined function ajax({type, url,data, dataType}){  return new Promise(function(open){  var xhr = new XMLHttpRequest()  xhr.onreadystatechange = function(){   if(xhr.readyState === 4 && xhr.status === 200){   if(dataType === "json"){    var res = JSON.parse(xhr.responseText)   }else{    var res = xhr.responseText   }   console.log(res)   open(res)   }  }  if(type === "get" && data !== undefined){   url += `?${data}`  }  xhr.open(type, url, true)  xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")  if(type === "get"){   xhr.send()  }else{   xhr.send(data)  }  }) } </script></body></html>

另:ajax實(shí)際代碼實(shí)現(xiàn)如下

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <script> var xhr = new XMLHttpRequest() xhr.onreadystatechange = function(){  if(xhr.readyState === 4 && xhr.status === 200){  console.log(xhr.responseText)  } } xhr.open("get", "http://localhost:3000", true) xhr.send() </script></body></html>

2.后端代碼

1) 創(chuàng)建一個后端項(xiàng)目

2) 在routes下創(chuàng)建index.js,users.js,代碼如下

// index.jsvar express = require("express");var router = express.Router();/* GET home page. */var products = [ { lid:1, pname:"筆記本", price:3400 }, { lid:2, pname:"手機(jī)", price:5400 }, { lid:3, pname:"iPad", price:6400 }]router.get("/", function(req, res, next) { res.send(products)});router.get("/details", function(req, res, next){ var lid = req.query.lid res.send(products[lid])})module.exports = router;
// user.jsvar express = require("express");var router = express.Router();/* GET users listing. */router.post("/", function(req, res, next) { var uname = req.body.uname var upwd = req.body.upwd if(uname === "lili" && upwd === "123456"){ res.send("登陸成功") }else{ res.send({  code: 0,  message: "用戶名或密碼錯誤" }) }});module.exports = router;

3.注:

為避免跨域,可將前端代碼和后端同時(shí)放在一個項(xiàng)目內(nèi),使用同一地址,再發(fā)送請求調(diào)取接口

到此這篇關(guān)于利用promise及參數(shù)解構(gòu)封裝ajax請求的文章就介紹到這了,更多相關(guān)promise封裝ajax請求內(nèi)容請搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!

標(biāo)簽: Ajax
主站蜘蛛池模板: 黄色三级欧美 | 亚洲国产第一区二区香蕉 | 国产成人精品区在线观看 | 国产一级真人毛爱做毛片 | 成 人 a v免费视频 | 亚洲一区二区三区在线网站 | 日本人成18在线播放 | 免费观看日本特色做爰视频在线 | 爱啪网亚洲第一福利网站 | 久久精品2 | 国产午夜伦伦伦午夜伦 | 国产成人亚洲精品久久 | 高清一区二区 | 99在线热视频只有精品免费 | 精品国产一区二区三区四区不 | 久草免费福利视频 | 未满14周岁啪啪网站 | 久久性生大片免费观看性 | 国产日韩高清一区二区三区 | 在线观看免费av网站 | 免费看欧美一级a毛片 | 香蕉成人在线视频 | 香蕉国产人午夜视频在线观看 | 欧美国产精品亚洲精品第一区 | 久久88香港三级台湾三级中文 | 99视频国产精品 | 日本aaa成人毛片 | 特黄毛片 | 亚洲免费久久 | 国语精品视频在线观看不卡 | 亚洲精品久久久久综合91 | 国产一区二区免费不卡在线播放 | 日韩 欧美 国产 师生 制服 | 加勒比综合网 | 日本理论片免费高清影视在线观看 | 国产又色又爽黄的网站免费 | 在线不卡一区二区三区日韩 | 免费观看一级一片 | chinese耄耋70老太性 | 超级香蕉97视频在线观看一区 | 成人国产精品一级毛片视频 |