如何使用promise及参数解构封装ajax请求

本文主要介绍如何使用promise及参数解构封装ajax请求的方法,文章介绍的很详细,应该对大家的学习和工作有一定的参考价值,有需要的朋友可以参考

1.前端代码

<!DOCTYPE html>

<html>

<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( www.bjpoppers.com )

  }else{

   xhr.send(data)

  }

  })

 }

 </script>

</body>

</html>

另:ajax实际代码实现如下

<!DOCTYPE html>

<html>

<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.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;

3.末:
为了避免跨域,可以在一个项目中同时放置前端代码和后端,使用相同的地址,再次发送 Request接口

这是一篇关于使用如何使用promise及参数解构封装ajax请求的文章,更多相关可关注脚本之家!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值