JavaScript学习笔记(9.4)

本文演示了如何使用Promise处理异步操作,特别是在fetchAPI中的应用。示例包括GET请求、POST请求以及其他HTTP方法(PUT、PATCH和DELETE),并强调了内容类型在发送JSON数据时的重要性。
<!--
 * @Author: RealRoad1083425287@qq.com
 * @Date: 2023-03-20 14:36:48
 * @LastEditors: Mei
 * @LastEditTime: 2023-03-20 14:42:27
 * @FilePath: \vscode\promise.html
 * @Description: 
 * 
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved. 
-->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var q=new Promise(function(resolve,reject){
            setTimeout(() => {
                resolve(["1","2","3"])//同意承诺

                //reject()  拒绝承诺
            }, 2000);
        })

        q.then(function(res){
            console.log("success",res)
        }).catch(function(){
            console.log("fail")
        })
    </script>
</body>
</html>

fetch(默认get方式)

   var username="xiaohong"
        fetch(`http://localhost:3000/users?username=${username}`).then(res=>{
            console.log(res)//res.json()  Promise对象
            if(res.ok){
            return res.json() }
            else{
                return Promise.reject({
                    a:1,
                    status:res.status,
                    statusText:res.statusText
                })
            }  //只有一个对象时,可以省略大括号和return
        }).then(res=>{
            console.log(res)
        })

post方式

 fetch("http://localhost:3000/users",{
            method:"POST",
            headers:{
                "content-type":"application/x-www-form-urlencoded"
            },
            body:"username=xiaohua&password=111"
        })
        .then(res=>res.json())
        .then(res=>{
            console.log(res)
        })

json格式

 fetch("http://localhost:3000/users",{
            method:"POST",
            headers:{
                "content-type":"application/json"
            },
            body:JSON.stringify({
            username:"mez",
            password:"478"})
        })
        .then(res=>res.json())
        .then(res=>{
            console.log(res)
        })

Put方式

 fetch("http://localhost:3000/users/2",{
            method:"PUT",
            headers:{
                "content-type":"application/json"
            },
            body:JSON.stringify({
            username:"mez",
            password:"478"})
        })
        .then(res=>res.json())
        .then(res=>{
            console.log(res)
        })

PATCH方式

 fetch("http://localhost:3000/users/3",{
            method:"PATCH",
            headers:{
                "content-type":"application/json"
            },
            body:JSON.stringify({
            username:"mez",
            password:"478"})
        })
        .then(res=>res.json())
        .then(res=>{
            console.log(res)
        })

delete方式

 fetch("http://localhost:3000/users/3",{
            method:"delete"
            
        })
        .then(res=>res.json())
        .then(res=>{
            console.log(res)
        })

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mez_Blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值