解決 ajax PUT、DELETE 請求失敗問題
阿新 • • 發佈:2018-12-08
RESTful 介面接收請求
@PostMapping
public Result add(@RequestBody CuUsers cuUsers) {
cuUsersService.save(cuUsers);
return ResultGenerator.genSuccessResult();
}
@DeleteMapping("/{id}")
public Result delete(@PathVariable Integer id) {
cuUsersService.deleteById(id);
return ResultGenerator.genSuccessResult();
}
@PutMapping
public Result update(@RequestBody CuUsers cuUsers) {
cuUsersService.update(cuUsers);
return ResultGenerator.genSuccessResult();
}
@GetMapping("/{id}")
public Result detail(@PathVariable Integer id) {
CuUsers cuUsers = cuUsersService.findById(id);
return ResultGenerator.genSuccessResult(cuUsers);
}
在AJAX中,type請求型別(get,post,delete,put)
在使用delete請求時,用data傳參,而不是在url中傳遞
JNHC.json({
url: "http://localhost:8081/cu/wotype/",
type:"post",
data:{
_method:"delete",
id:id
},
callback: function (data ) {
alert("刪除成功!");
showTable();
}
})
data中新增_method引數,type型別為post;
在springboot 微服務中 需要在 @DeleteMapping(“/{id}”) 上放加@CrossOrigin註解;