首页 > 解决方案 > 将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 "";
    }
}

标签: javaajaxspring-boot

解决方案


RequestBody当您将其作为GET请求发送时,您为什么要使用它。我会POST改用。用于@PostMapping您的资源并进行POSTajax 调用。


推荐阅读