av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

利用promise及參數解構封裝ajax請求的方法

瀏覽:270日期: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", 如果服務端返回的是json格式字符串,就通過dataType通知ajax函數自動轉換為對象  * **/ ajax({  type: "get",  url: "http://localhost:3000",  dataType: "json" }) // data 不寫在解構時值默認為 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 不寫在解構時值默認為 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實際代碼實現如下

<!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) 創建一個后端項目

2) 在routes下創建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:"手機", 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.注:

為避免跨域,可將前端代碼和后端同時放在一個項目內,使用同一地址,再發送請求調取接口

到此這篇關于利用promise及參數解構封裝ajax請求的文章就介紹到這了,更多相關promise封裝ajax請求內容請搜索以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持!

標簽: Ajax
主站蜘蛛池模板: 六月婷婷综合 | 久久亚洲国产 | 婷婷中文字幕 | 欧美午夜精品一区二区三区 | 国产视频一区在线 | 激情五月综合色婷婷一区二区 | av不卡在线播放 | 亚洲激情在线观看 | av色在线| 蜜臀久久99精品久久久久宅男 | 国产欧美精品一区二区色综合 | 成人在线精品 | 天天拍夜夜操 | 玖玖精品视频 | av黄色在线 | 色哟哟入口国产精品 | 免费观看一区二区 | 国产三级在线观看视频 | 欧美日韩免费 | 免费在线观看黄 | 中文日韩欧美 | 色综合天天综合网国产成人网 | 国产日韩欧美一区二区 | 国产综合视频在线观看 | 超碰在线观看97 | 日韩欧美一区在线 | 国产午夜精品一区二区三区嫩草 | 成人av播放 | 亚洲一区二区三区中文字幕 | 日本精品中文字幕 | 日韩一区二区三区av | 午夜无遮挡 | 欧美精品一二三 | 中文字幕理伦片免费看 | 中文字幕国产 | 午夜a级片| 欧美日韩国产在线 | 九九国产精品视频 | 日韩一区二区在线视频 | 欧美精品国产 | 综合色av|