日日操夜夜添-日日操影院-日日草夜夜操-日日干干-精品一区二区三区波多野结衣-精品一区二区三区高清免费不卡

公告:魔扣目錄網(wǎng)為廣大站長提供免費收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.ylptlb.cn 】, 免友鏈快審服務(wù)(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

這篇文章主要介紹了利用promise及參數(shù)解構(gòu)封裝ajax請求的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

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', 如果服務(wù)端返回的是json格式字符串,就通過dataType通知ajax函數(shù)自動轉(zhuǎn)換為對象
 **/
ajax({
    type: 'get',
    url: 'http://localhost:3000',
    dataType: 'json'
})
// data 不寫在解構(gòu)時值默認(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=lili&upwd=123456',
}).then(function(res){
    alert(res)
})
// dataType 不寫在解構(gòu)時值默認(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實際代碼實現(xiàn)如下

<!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) 創(chuà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;

3、注:

為避免跨域,可將前端代碼和后端同時放在一個項目內(nèi),使用同一地址,再發(fā)送請求調(diào)取接口。



分享到:
標(biāo)簽:promise 參數(shù)解構(gòu) 封裝ajax請求
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨大挑戰(zhàn)2018-06-03

數(shù)獨一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定