怎么利用promise及参数解构封装ajax请求

这篇文章主要介绍怎么利用promise及参数解构封装ajax请求,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联公司长期为成百上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为昆明企业提供专业的成都网站设计、成都网站建设,昆明网站改版等技术服务。拥有十年丰富建站经验和众多成功案例,为您定制开发。

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) 创建一个后端项目

怎么利用promise及参数解构封装ajax请求

2) 在routes下创建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:'手机',
 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: '用户名或密码错误'
 })
 }
});

module.exports = router;

3.注:

为避免跨域,可将前端代码和后端同时放在一个项目内,使用同一地址,再发送请求调取接口

以上是“怎么利用promise及参数解构封装ajax请求”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!

标题名称:怎么利用promise及参数解构封装ajax请求
文章来源:https://www.cdcxhl.com/article32/posdpc.html

成都网站建设公司_创新互联,为您提供虚拟主机域名注册网站收录ChatGPT网站营销面包屑导航

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都网页设计公司