原生html 点击按钮删除数据案例

--------此篇为个人错误记录,也希望能对大家有点帮助---------

         用json-server服务器,json文件当作数据库,然后ajax访问的是json文件的地址,一运行发现报错。

错误描述:

        当我点击按钮是对应的li标签可以删除成功,对应的数据无法删除,搜索原因可能是跨域问题。我也没后端怎么能跨域不一样呢(跨域也可能是别的意思,说的不准见谅)

错误原因

        又双叒叕打错字了。。删除DELETE打成DELECT

下面是成功的代码


        import ajax from '../util.js'
        //原型
        class Todolist{
            constructor(select){
                this.list=document.querySelector(select)
                this.listdata = []//列表数组的数据
                this.init()
            }
            init(){
                //初始化
                this.bindEvent()
                //获取数据
                this.getList()
            }
            bindEvent(){
                this.list.onclick=(evt)=>{
                    console.log(evt.target)
                    if(evt.target.nodeName=="BUTTON"){
                       this.removeItem(evt.target)
                    }
                }
            }
            getList(){
                //获取
                ajax({
                    url:"http://localhost:3000/List",
                    success:(res)=>{
                        console.log(this)
                        this.listdata =res
                     
                        this.render()
                    },
                    error:function(){

                    }
                })
            }
            //渲染页面
            render(){
              this.list.innerHTML=this.listdata.map
              (item=>`<li>
                ${item.text}<button data-index=${item.id}>删除</button>
                </li>`).join("")
            }
            addItem(){

            }
            removeItem(target){
                target.parentNode.remove()
                console.log(target.dataset.index)
                //删除
                ajax({
                    url:`http://localhost:3000/List/${target.dataset.index}`,
                    method:"DELETE",
                    success:(res)=>{
                        
                        console.log("成功删除")
                    },
                    error:function(){
                        console.log(res,"失败了")
                    }
                })
            }
            updateItem(){

            }
        }
        var obj = new Todolist(".list")
        console.log(obj)
  

ajax这里DELETE写错了

 ajax({
                    url:`http://localhost:3000/List/${target.dataset.index}`,
                    method:"DELECT",
                    success:(res)=>{
                        
                        console.log("成功删除")
                    },
                    error:function(){
                        console.log(res,"失败了")
                    }
                })

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值