佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法

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

熱門標(biāo)簽:電子圍欄 Linux服務(wù)器 Mysql連接數(shù)設(shè)置 銀行業(yè)務(wù) 服務(wù)器配置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 團(tuán)購(gòu)網(wǎng)站 阿里云

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=liliupwd=123456
  * dataType: '' / 'json', 如果服務(wù)端返回的是json格式字符串,就通過(guò)dataType通知ajax函數(shù)自動(dòng)轉(zhuǎn)換為對(duì)象
  * **/
 ajax({
  type: 'get',
  url: 'http://localhost:3000',
  dataType: 'json'
 })
 // data 不寫(xiě)在解構(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=liliupwd=123456',
 }).then(function(res){
  alert(res)
 })
 // dataType 不寫(xiě)在解構(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)建一個(gè)后端項(xiàng)目

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

// index.js
var 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.js
var 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: '用戶名或密碼錯(cuò)誤'
 })
 }
});

module.exports = router;

3.注:

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

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

您可能感興趣的文章:
  • 詳解JavaScript原生封裝ajax請(qǐng)求和Jquery中的ajax請(qǐng)求
  • vue 組件的封裝之基于axios的ajax請(qǐng)求方法
  • 原生js封裝的ajax方法示例
  • 純js封裝的ajax功能函數(shù)與用法示例
  • 詳解自定義ajax支持跨域組件封裝
  • react中的ajax封裝實(shí)例詳解
  • 基于ajax和jsonp的原生封裝(實(shí)例)
  • 使用原生js封裝的ajax實(shí)例(兼容jsonp)
  • vue-ajax小封裝實(shí)例
  • 如何封裝一個(gè)Ajax函數(shù)

標(biāo)簽:棗莊 衢州 衡水 大理 萍鄉(xiāng) 廣元 江蘇 蚌埠

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    高唐县| 商都县| 博罗县| 五大连池市| 洪雅县| 双柏县| 中宁县| 黄石市| 云霄县| 台北县| 青阳县| 三河市| 英山县| 富裕县| 饶平县| 乐至县| 余江县| 海原县| 怀宁县| 英德市| 洪湖市| 抚顺县| 宣武区| 乐昌市| 通海县| 陇西县| 岳池县| 渝中区| 冕宁县| 淮南市| 诸暨市| 永年县| 彭州市| 宜黄县| 广平县| 佛冈县| 阿城市| 抚州市| 乐平市| 溧水县| 乡城县|