关于ajax form-data和json的区别,及其application/x-www-form-urlencoded;charset=UTF-8问题解决方案
json
适用于现代 C++ 的 JSON。
项目地址:https://gitcode.com/gh_mirrors/js/json
免费下载资源
·
常见的ajax传值有两种方式
- form-data
$.ajax({
type: "post",
url: ctx + "login",
data: {
"username": username,
"password": password,
"validateCode" : validateCode,
"rememberMe": rememberMe
},
success: function(r) {
if (r.code == 0) {
debugger;
location.href = ctx + 'index';
} else {
$.modal.closeLoading();
$('.imgcode').click();
$(".code").val("");
$.modal.msg(r.msg);
}
}
});
此时Java后端需要这样接收
@PostMapping("/login")
@ResponseBody
public AjaxResult ajaxLogin(String username, String password, Boolean rememberMe)
{
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
Subject subject = SecurityUtils.getSubject();
try
{
subject.login(token);
return success();
}
catch (AuthenticationException e)
{
String msg = "用户或密码错误";
if (StringUtils.isNotEmpty(e.getMessage()))
{
msg = e.getMessage();
}
return error(msg);
}
}
- json
$.ajax({
type: "post",
dataType: "json",
url: ctx + "login",
data: JSON.stringify({"username": username, "password": password,"validateCode" : validateCode,"rememberMe": rememberMe}),
contentType: "application/json;charset-UTF-8",
success: function(r) {
if (r.code == 0) {
debugger;
location.href = ctx + 'index';
} else {
$.modal.closeLoading();
$('.imgcode').click();
$(".code").val("");
$.modal.msg(r.msg);
}
}
});
此时Java后端需要这样接收
@PostMapping("/login")
@ResponseBody
public AjaxResult ajaxLogin(@RequestBody Test test)
{
UsernamePasswordToken token = new UsernamePasswordToken(test.getUsername(), test.getPassword(), test.getRememberMe());
Subject subject = SecurityUtils.getSubject();
try
{
subject.login(token);
return success();
}
catch (AuthenticationException e)
{
String msg = "用户或密码错误";
if (StringUtils.isNotEmpty(e.getMessage()))
{
msg = e.getMessage();
}
return error(msg);
}
}
GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
2d42229f
* Support BSON uint64 de/serialization
Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>
* Treat 0x11 as uint64 and not timestamp specific
Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com>
---------
Signed-off-by: Michael Valladolid <mikevalladolid@gmail.com> 4 天前
1809b3d8
Signed-off-by: Niels Lohmann <mail@nlohmann.me> 5 天前
更多推荐
已为社区贡献1条内容
所有评论(0)