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 "";
}
}
# 1 楼答案
为什么在以
GET
请求发送时使用RequestBody
。我会用POST
来代替。 使用@PostMapping
作为您的资源,并进行POST
ajax调用