axios请求:
前端:home.jsxconst params = {
idArr: JSON.stringify(this.state.selectedRowKeys) } const _this = this; axios.post(`http://localhost:5555/api/delete_blogs`, qs.stringify(params)).then((resp) => { if (resp.data.ret) { notification['success']({ message: '成功提示', description: resp.data.msg, }) _this.getData(0, 5); } else { notification['error']({ message: '错误提示', description: resp.data.msg, }) } }, (err) => { notification['error']({ message: '错误提示', description: '网络错误,删除失败!' }) });
app.post("/api/delete_blogs", (req, res) => { let obj = { ret: false, msg: "" } var idArr = JSON.parse(req.body.idArr); for(let i = 0; i<idArr.length; i++){ connection.query(`DELETE FROM blog WHERE id='${idArr[i]}'`, (error, results, fields) => { if(!error){ obj.ret = true; obj.msg = "删除成功!"; res.header("Access-Control-Allow-Origin", "*"); res.type('application/json'); res.jsonp(obj); }else{ next(error); } }) } })
前端:
function deleteBackupTask(id){ alertify.confirm('确定删除吗?', function (e) { if (e) { // delete begin,asyncAjax是一个封装Jquery的Promise tool.asyncAjax('/api/delete_backup_task', 'DELETE', {id:id}).then(result => { if (result.ret) { toastr.success(result.msg, "成功提示"); $backupTaskTable.bootstrapTable("refresh"); } else { toastr.error(result.message, "错误提示"); } }).catch(error => { console.log(error); }) // delete end } else { // user clicked "cancel" } }); }
后端:
// 删除 app.delete("/delete", (req, res) => { var dataObj = { ret: false, msg: "" }; var idArr = JSON.parse(req.body.userIdArr); fs.readFile("./file/obj.txt", function (error, data) { if (!error) { var userArr = JSON.parse(data); for (var i = 0; i < userArr.length; i++) { for (var n = 0; n < idArr.length; n++) { if (userArr[i].id === idArr[n]) { userArr.splice(i, 1); } } } fs.writeFile("./file/obj.txt", JSON.stringify(userArr), function (error, data) { if (!error) { dataObj.ret = true; dataObj.msg = "删除用户成功!"; res.json(dataObj); } else { dataObj.msg = "写入文件失败!"; res.json(dataObj); } }) } else { dataObj.msg = "读取文件失败!"; res.json(dataObj); } }) })