0

I'm trying to passing Json Data with Ajax to SpringBoot, but it's throwing error, What is the mistake in my program Pls tell me and suggest me. what is the mistake.

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));
        }
    });

Controller Class

@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 "";
    }
}
Anish B.
  • 7,321
  • 3
  • 12
  • 33
Ng Sharma
  • 1,736
  • 4
  • 18
  • 33
  • Does this answer your question? [How to pass Json object from ajax to spring mvc controller?](https://stackoverflow.com/questions/20245544/how-to-pass-json-object-from-ajax-to-spring-mvc-controller) – Anish B. Jun 12 '20 at 18:14

1 Answers1

0

Why are you using RequestBody when you are sending it as GET request. I would have used POST instead. Use @PostMapping for your resource and make a POST ajax call.

Nish
  • 595
  • 2
  • 19