<el-row slot-scope="scope">
<el-button type="danger" @click.native.prevent="deleteById(scope.$index, brand)" >删除el-button>
el-row>
<script>
new Vue({
el: "#app",
// 删除
deleteById(row){
this.selectedid=row;
this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
var _this= this;
// 发送ajax请求,添加数据
axios({
method:"post",
url:"http://localhost:8080/brand-case/brand/deleteById",
data:_this.selectedid
}).then(function (resp) {
if (resp.data == "success" ){
// 删除成功
// 重新查询数据
_this.selectAll();
// 弹出消息提示
_this.$message({
message:"删除成功",
type:"success"
});
}
})
}).catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
});
});
this.selectedid=''
},
data() {
return {
selectedid:'',
// 品牌模型数据
brand: {
status: '',
brandName: '',
companyName: '',
id:"",
ordered:"",
description:""
}
}
}
})
//后端代码
删除
public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException{
// BufferedReader reader = req.getReader();
// String params = reader.readLine();
//
//
//
// Brand brand = JSON.parseObject(params, Brand.class);
// brandService.delete(brand.getId());
// String id = req.getParameter("id");
// System.out.println(id);
// 删除成功返回成功标志
req.setCharacterEncoding("UTF-8");
BufferedReader reader = req.getReader();
String params = reader.readLine();
System.out.println(params);
int id = JSON.parseObject(params, int.class);
brandService.deleteById(id);
resp.getWriter().write("success");
}
代码运行一直提示我获取到的值为null
在前端的 deleteById 方法中,调用了 deleteById(scope.$index, brand),但是在后台删除方法中,获取参数的方式是通过读取请求体中的参数,即通过 BufferedReader reader = req.getReader(); 读取请求体中的数据。而在前端代码中没有传递任何请求体中的数据。因此,获取到的值为 null。
解决方法是需要将要传递的参数作为 data 属性传递给 axios 函数,例如:axios({method:"post", url:"http://localhost:8080/brand-case/brand/deleteById", data: {id: _this.selectedid}})。同时,在后台的接收方法中,可以使用 req.getParameter("id") 获取前端传递的参数。
json解析的问题,看看json是什么内容,还有字符编码问题
如果要查询表的NULL需要使用 is null
或is not null
,如果直接使用=/!=/in/not inl
将查询不到值
mysql> SELECT * FROM `user`;
+----+------+-----+---------+
| id | name | age | address |
+----+------+-----+---------+
| 1 | wyf | 32 | 合肥 |
| 2 | xx | 31 | 北京 |
| 3 | yy | 30 | 上海 |
| 4 | zz | 11 | |
| 5 | aa | 21 | NULL |
+----+------+-----+---------+
5 rows in set
mysql> SELECT * FROM `user` WHERE address IS NULL;
+----+------+-----+---------+
| id | name | age | address |
+----+------+-----+---------+
| 5 | aa | 21 | NULL |
+----+------+-----+---------+
1 row in set
mysql> SELECT * FROM `user` WHERE address = NULL;
Empty set
mysql>