写一个用ajax的注册页面代码 1.返回的json字符串, 2.对应springboot的controller代码 3. function (data) { }
时间: 2024-04-29 19:25:47 浏览: 15
1. 这是一个用ajax的注册页面代码的示例:
```
$(document).ready(function() {
$("#register-form").submit(function(event) {
// 阻止表单默认提交行为
event.preventDefault();
// 获取表单数据
var username = $("#username").val();
var password = $("#password").val();
// 发送ajax请求
$.ajax({
url: "/register",
type: "POST",
data: JSON.stringify({username: username, password: password}),
contentType: "application/json",
dataType: "json",
success: function(data) {
// 处理返回的json字符串
console.log(data);
}
});
});
});
```
2. 对应的SpringBoot的Controller代码:
```
@RestController
public class UserController {
@PostMapping("/register")
public ResponseEntity<String> register(@RequestBody User user) {
// 处理注册逻辑
// ...
// 返回JSON字符串
return ResponseEntity.ok("{\"status\": \"success\"}");
}
}
```
3. function(data) { } 是ajax的回调函数,用于处理服务器返回的数据。在这个例子中,我们可以在success回调函数中处理返回的JSON字符串,如:
```
success: function(data) {
console.log(data.status);
}
```