--------此篇为个人错误记录,也希望能对大家有点帮助---------
用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,"失败了") } })