有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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) 个答案

  1. # 1 楼答案

    为什么在以GET请求发送时使用RequestBody。我会用POST来代替。 使用@PostMapping作为您的资源,并进行POSTajax调用