java - 将json数据ajax传递给Spring boot
问题描述
我正在尝试将带有 Ajax 的 Json 数据传递给 SpringBoot,但它会抛出错误,我的程序中有什么错误请告诉我并建议我。什么是错误。
var emailId = {"emailId" : userEmail};
$.ajax({
url : "/api/v1/leadsquard/user/emailId",
type : "GET",
data : JSON.stringify(emailId),
dataType : "text",
contentType: "application/json",
success: function (response) {
alert("Success "+ JSON.stringify(response));
},
error: function(response) {
alert("Success "+ JSON.stringify(response));
}
});
控制器类
@RestController
@RequestMapping(value = "/api/v1")
public class LeadSquardController {
@Autowired
LeadSquardService leadSquardService;
@GetMapping("leadsquard/user/emailId")
@ResponseBody
public String getByEmailaddress(@RequestBody Object emailId) {
System.out.println("Email : " + emailId.getClass().getName()); //Testing line
System.out.println("Email : " + emailId); //Testing line
return "";
}
}
解决方案
RequestBody
当您将其作为GET
请求发送时,您为什么要使用它。我会POST
改用。用于@PostMapping
您的资源并进行POST
ajax 调用。
推荐阅读
- python-3.x - 如何使用 wxPython 制作窗口覆盖(在浏览器、游戏除外)
- python - 使用 pandas 连接多个数据帧
- php - 我如何阅读正确的 http 状态码
- c++ - 共享指针交换方法标识更改
- json - 快速将json解码类型传递给http请求
- hibernate - 如何修复我遇到的“LazyInitializationException”?
- html - 当输入字段为焦点输入字段来自另一个div时如何更改图标颜色 - HTML - CSS
- ios - cordova / ios / 混合应用程序 - UITableView 加载项目不正确且无序
- c++ - HPX transform_reduce
- javascript - MutationObserver 不会在用户脚本中触发属性更改 - 有什么想法吗?