json-server对数据进行增删改查操作

简介: json-server对数据进行增删改查操作

在上一节里面,成功获取json-server模拟后端接口

https://www.jianshu.com/p/920d73fc32de

准备工作,先打开cmd,运行以上文章里内容:

现在要对这些接口进行增删改查操作,先写增删改查的按钮,进行点击事件 的操作。

<button type="button" class="add">添加</button>
<button type="button" class="delete">删除</button>
<button type="button" class="edit">修改</button>
<button type="button" class="serch">查询</button>
1:请求数据
//请求用户数据
        $.ajax({
            type:"GET",
            url:"http://localhost:3000/users",
            dataType:"json",
            success:function (data) {
                    console.log(data, '请求成功')
            },
            error:function (err) {
                    console.log(data, '请求失败')
            }
        }); 

2:增加数据
    $(".add").click(function() {
    var newData = {
        "name": "新加数据",
        "phone": "123456789",
        "email": "11357097537@qq.com",
        "age": "45",
        "id": 5,
        "companyId": 3
    };
    $.ajax({
        type: "post",
        url: "http://localhost:3000/users",
        data: newData,
        success: function(data) {
            console.log(data, '请求成功')
        },
        error: function(data) {
            console.log(data, '请求失败')
        }
    })
})

打开db.json数据,发现里面添加了一条数据


3:删除数据

删除id为2的这条数据


$(".delete").click(function() {
        var delUserId=4;
            $.ajax({
                type:"DELETE",
                url:"http://localhost:3000/users/"+delUserId,
                dataType:"json",
                success:function(data){
                    console.log(data)
                    alert("删除成功")
                },
                error:function(err){
                    console.error(err)
                }
            })
})
4:修改数据
$(".edit").click(function() {
    var updateuser = {
        "name": "王小婷要修改一下哦",
        "phone": "123456789",
        "email": "11357097537@qq.com",
        "age": "20",
        "id": 1,
        "companyId": 1
    };
    $.ajax({
        type: "PUT",
        url: "http://localhost:3000/users/1",
        data: updateuser,
        success: function(data) {
            console.log(data)
        },
        error: function(err) {
            console.error(err)
        }
    })
})

5:查询数据

查询id=1的数据

$(".serch").click(function() {
            //查询id=1
            $.ajax({
                type: "get",
                url: "http://localhost:3000/users/1",
                dataType: "json",
                success: function(e) {
                    console.log(e, '请求成功')
                },
                error: function(e) {
                    console.log(e, '请求失败')
                }
            })
        })

参考demo

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <button type="button" class="add">添加</button>
        <button type="button" class="delete">删除</button>
        <button type="button" class="edit">修改</button>
        <button type="button" class="serch">查询</button>
    </body>
    <script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
    <script>
        //增加
        $(".add").click(function() {
            var newData = {
                "name": "新加数据",
                "phone": "123456789",
                "email": "11357097537@qq.com",
                "age": "45",
                "id": 5,
                "companyId": 3
            };
            $.ajax({
                type: "post",
                url: "http://localhost:3000/users",
                data: newData,
                success: function(data) {
                    console.log(data)
                    alert("添加成功")
                },
                error: function(data) {
                    console.log(data)
                    alert("已存在,不可重复添加")
                }
            })
        })
        //删除
        $(".delete").click(function() {
            var delUserId = 5;
            $.ajax({
                type: "DELETE",
                url: "http://localhost:3000/users/" + delUserId,
                dataType: "json",
                success: function(data) {
                    console.log(data)
                    alert("删除成功")
                },
                error: function(err) {
                    alert("不可重复删除")
                }
            })
        })
        //修改
        $(".edit").click(function() {
            var updateuser = {
                "name": "王小婷要修改一下哦",
                "phone": "123456789",
                "email": "11357097537@qq.com",
                "age": "20",
                "id": 1,
                "companyId": 1
            };
            $.ajax({
                type: "PUT",
                url: "http://localhost:3000/users/1",
                data: updateuser,
                success: function(data) {
                    console.log(data)
                    alert("修改成功")
                },
                error: function(err) {
                    alert("修改失败")
                }
            })
        })
        //查询
        $(".serch").click(function() {
            //查询id=1
            $.ajax({
                type: "get",
                url: "http://localhost:3000/users/1",
                dataType: "json",
                success: function(data) {
                    console.log(data)
                    alert("查询成功")
                },
                error: function(data) {
                    console.log(data, '请求失败')
                    alert("查询失败")
                }
            })
        })
    </script>
</html



相关文章
|
3月前
|
JSON API 数据安全/隐私保护
深度分析淘宝卖家订单详情API接口,用json返回数据
淘宝卖家订单详情API(taobao.trade.fullinfo.get)是淘宝开放平台提供的重要接口,用于获取单个订单的完整信息,包括订单状态、买家信息、商品明细、支付与物流信息等,支撑订单管理、ERP对接及售后处理。需通过appkey、appsecret和session认证,并遵守调用频率与数据权限限制。本文详解其使用方法并附Python调用示例。
|
1月前
|
JSON API 数据格式
淘宝拍立淘按图搜索API系列,json数据返回
淘宝拍立淘按图搜索API系列通过图像识别技术实现商品搜索功能,调用后返回的JSON数据包含商品标题、图片链接、价格、销量、相似度评分等核心字段,支持分页和详细商品信息展示。以下是该API接口返回的JSON数据示例及详细解析:
|
1月前
|
JSON 算法 API
Python采集淘宝商品评论API接口及JSON数据返回全程指南
Python采集淘宝商品评论API接口及JSON数据返回全程指南
|
2月前
|
机器学习/深度学习 JSON 监控
淘宝拍立淘按图搜索与商品详情API的JSON数据返回详解
通过调用taobao.item.get接口,获取商品标题、价格、销量、SKU、图片、属性、促销信息等全量数据。
|
1月前
|
JSON API 数据安全/隐私保护
Python采集淘宝拍立淘按图搜索API接口及JSON数据返回全流程指南
通过以上流程,可实现淘宝拍立淘按图搜索的完整调用链路,并获取结构化的JSON商品数据,支撑电商比价、智能推荐等业务场景。
|
2月前
|
JSON 缓存 自然语言处理
多语言实时数据微店商品详情API:技术实现与JSON数据解析指南
通过以上技术实现与解析指南,开发者可高效构建支持多语言的实时商品详情系统,满足全球化电商场景需求。
|
2月前
|
JSON API 数据格式
干货满满!淘宝商品详情数据,淘宝API(json数据返回)
淘宝商品详情 API 接口(如 taobao.item.get)的 JSON 数据返回示例如下
|
3月前
|
JSON 算法 安全
淘宝商品详情API接口系列,json数据返回
淘宝开放平台提供了多种API接口用于获取商品详情信息,主要通过 淘宝开放平台(Taobao Open Platform, TOP) 的 taobao.tbk.item.info.get(淘宝客商品详情)或 taobao.item.get(标准商品API)等接口实现。以下是关键信息及JSON返回示例:
|
1月前
|
JSON 中间件 Java
【GoGin】(3)Gin的数据渲染和中间件的使用:数据渲染、返回JSON、浅.JSON()源码、中间件、Next()方法
我们在正常注册中间件时,会打断原有的运行流程,但是你可以在中间件函数内部添加Next()方法,这样可以让原有的运行流程继续执行,当原有的运行流程结束后再回来执行中间件内部的内容。​ c.Writer.WriteHeaderNow()还会写入文本流中。可以看到使用next后,正常执行流程中并没有获得到中间件设置的值。接口还提供了一个可以修改ContentType的方法。判断了传入的状态码是否符合正确的状态码,并返回。在内部封装时,只是标注了不同的render类型。再看一下其他返回的类型;
157 3
|
1月前
|
JSON Java Go
【GoGin】(2)数据解析和绑定:结构体分析,包括JSON解析、form解析、URL解析,区分绑定的Bind方法
bind或bindXXX函数(后文中我们统一都叫bind函数)的作用就是将,以方便后续业务逻辑的处理。
264 3