利用promise及参数解构封装ajax请求的方法

 4493

这篇文章主要介绍了利用promise及参数解构封装ajax请求的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

1、前端代码

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. /**
  11.  * type: get/post
  12.  * url: http://localhost:3000 http://localhost:3000/details http://localhost:3000/users
  13.  * data: lid=5 / uname=lili&upwd=123456
  14.  * dataType: '' / 'json', 如果服务端返回的是json格式字符串,就通过dataType通知ajax函数自动转换为对象
  15.  **/
  16. ajax({
  17.     type: 'get',
  18.     url: 'http://localhost:3000',
  19.     dataType: 'json'
  20. })
  21. // data 不写在解构时值默认为 data: undefined
  22. ajax({
  23.     type: 'get',
  24.     url: 'http://localhost:3000/details',
  25.     data: 'lid=0',
  26.     dataType: 'json'
  27. })
  28. ajax({
  29.     type: 'post', 
  30.     url: 'http://localhost:3000/users', 
  31.     data: 'uname=lili&upwd=123456',
  32. }).then(function(res){
  33.     alert(res)
  34. })
  35. // dataType 不写在解构时值默认为 dataType: undefined
  36. function ajax({type, url,data, dataType}){
  37.     return new Promise(function(open){
  38.         var xhr = new XMLHttpRequest()
  39.         xhr.onreadystatechange = function(){
  40.             if(xhr.readyState === 4 && xhr.status === 200){
  41.                 if(dataType === 'json'){
  42.                     var res = JSON.parse(xhr.responseText)
  43.                 } else {
  44.                     var res = xhr.responseText
  45.                 }
  46.                 console.log(res)
  47.                 open(res)
  48.             }
  49.         }
  50.         if(type === 'get' && data !== undefined){
  51.             url += `?${data}`
  52.         }
  53.         xhr.open(type, url, true)
  54.         xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded')
  55.         if (type === 'get') {
  56.             xhr.send()
  57.         } else {
  58.             xhr.send(data)
  59.         }
  60.     })
  61. }
  62. </script>
  63. </body>
  64. </html>

另:ajax实际代码实现如下

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. var xhr = new XMLHttpRequest()
  11. xhr.onreadystatechange = function(){
  12.     if(xhr.readyState === 4 && xhr.status === 200){
  13.         console.log(xhr.responseText)
  14.     }
  15. }
  16. xhr.open('get', 'http://localhost:3000', true)
  17. xhr.send()
  18. </script>
  19. </body>
  20. </html>

2、后端代码

1) 创建一个后端项目


在这里插入图片描述


2) 在routes下创建index.js,users.js,代码如下

  1. // index.js
  2. var express = require('express');
  3. var router = express.Router();
  4. /* GET home page. */
  5. var products = [
  6.     {
  7.         lid:1,
  8.         pname:'笔记本',
  9.         price:3400
  10.     },
  11.     {
  12.         lid:2,
  13.         pname:'手机',
  14.         price:5400
  15.     },
  16.     {
  17.         lid:3,
  18.         pname:'iPad',
  19.         price:6400
  20.     }
  21. ]
  22. router.get('/', function(req, res, next) {
  23.     res.send(products)
  24. });
  25. router.get('/details', function(req, res, next){
  26.     var lid = req.query.lid
  27.     res.send(products[lid])
  28. })
  29. module.exports = router;

3、注:

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



TAG标签:
本文网址:https://www.zztuku.com/detail-8751.html
站长图库 - 利用promise及参数解构封装ajax请求的方法
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐