First of all, note that @ requestbody accepts the JSON string
so write this
dataType:"json",
contentType: 'application/json',
data: JSON.stringify(data.field),
Instead of @ requestbody, you can directly receive the JSON type
dataType:"json",
data: data.field,
The situation of not using @ requestbody
front end page
$.ajax({
url: "http://localhost:8081/role//saveOrUpdate",
method:"post",
dataType:"json",
// contentType: 'application/json',
data: data.field,
success(data){
console.log("======================")
console.log(data.field)
if(data.code == 200){
layer.msg('add success', function () {
window.location = 'list.html';
});
}
},
error(data){
console.log(data)
if(data.code != 200){
layer.msg(data);
}
}
});
Back end:
@PostMapping("/saveOrUpdate")
public Result saveOrUpdate(Roles roles){
System.out.println(roles);
boolean saveOrUpdate = roleService.saveOrUpdate(roles);
if(saveOrUpdate == true)
return Result.succ(null);
else
return Result.fail("failed to add");
}
Using @ requestbody
front end:
$.ajax({
url: "http://localhost:8081/role//saveOrUpdate",
method:"post",
dataType:"json",
contentType: 'application/json',
data: JSON.stringify(data.field),
success(data){
console.log("======================")
console.log(data.field)
if(data.code == 200){
layer.msg('add success', function () {
window.location = 'list.html';
});
}
},
error(data){
console.log(data)
if(data.code != 200){
layer.msg(data);
}
}
});
back-end
@PostMapping("/saveOrUpdate")
public Result saveOrUpdate(@RequestBody Roles roles){
System.out.println(roles);
boolean saveOrUpdate = roleService.saveOrUpdate(roles);
if(saveOrUpdate == true)
return Result.succ(null);
else
return Result.fail("failed to add");
}
Read More:
- How to use Runtime.getRuntime().Exec()
- Vue3.0 Use el-dialog visible Error: ‘.sync‘ modifier on ‘v-bind‘ directive is deprecated. Use ‘v-model:pro
- Vue2.0: How to Use vue3 api to encapsulate Axios
- [Solved] Warning: To load an ES module, set “type“: “module“ in the package.json or use the .mjs extension
- How to use HPQC on Chrome browser
- JS: How to Solve split, join, toString Use error
- How to Use DOM to operate CSS
- Javascript: How to Use Date.Now() to implement front-end frequency limit
- [Solved] gyp ERR! stack Error: Could not find any Python installation to use
- Vue Use scss Error: this.getOptions is not a function [How to Solve]
- If the request parameter is formdata, use the Ajax operation
- [Solved] Error while trying to use the following icon from the Manifest
- [Solved] SyntaxError: Cannot use import statement outside a module
- After Vite starts, it will prompt “network: use ` — host ` to expose”, and the service cannot be accessed through network IP
- [Solved] electron Use remote Error: Cannot read properties of undefined (reading ‘BrowserWindow‘)
- [Solved] Vue3 Error: Cannot use ‘in‘ operator to search for ‘path‘ in undefined
- [Solved] SyntaxError: Cannot use import statement outside a module
- Use of $watch in Vue (solve rangeerror: maximum call stack size exceeded)
- [Solved] SyntaxError: Cannot use import statement outside a module
- [error] IDE service port disabled. To use CLI Call, please enter y to confirm enabling CLI capabilit