Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Json object from ajax to spring mvc controller?

I am working on SpringMVC, I am passing data from ajax to controller but i got null value in my controller please check my code below

function searchText()
{
   var sendData = {
    "pName" : "bhanu",
     "lName" :"prasad"
   }
  $.ajax({
type: "POST",
 url: "/gDirecotry/ajax/searchUserProfiles.htm,
    async: true,
    data:sendData,
    success :function(result)
    {
    }
 }

MyControllerCode

         RequestMapping(value="/gDirecotry/ajax/searchUserProfiles.htm",method=RequestMethod.POST)

       public  @ResponseBody String  getSearchUserProfiles(HttpServletRequest request)
       {
         String pName = request.getParameter("pName");
         //here I got null value
       }

any one help me

like image 633
user2963481 Avatar asked Nov 27 '13 14:11

user2963481


2 Answers

Hey enjoy the following code.

Javascript AJAX call

function searchText() {
   var search = {
      "pName" : "bhanu",
      "lName" :"prasad"
   }

   $.ajax({
       type: "POST",
       contentType : 'application/json; charset=utf-8',
       dataType : 'json',
       url: "/gDirecotry/ajax/searchUserProfiles.html",
       data: JSON.stringify(search), // Note it is important
       success :function(result) {
       // do what ever you want with data
       }
   });
}

Spring controller code

RequestMapping(value="/gDirecotry/ajax/searchUserProfiles.htm",method=RequestMethod.POST)

public  @ResponseBody String  getSearchUserProfiles(@RequestBody Search search, HttpServletRequest request) {
    String pName = search.getPName();
    String lName = search.getLName();

    // your logic next
}

Following Search class will be as follows

class Search {
    private String pName;
    private String lName;

    // getter and setters for above variables
}

Advantage of this class is that, in future you can add more variables to this class if needed.
Eg. if you want to implement sort functionality.

like image 100
Vijay Shegokar Avatar answered Sep 28 '22 05:09

Vijay Shegokar


Use this method if you dont want to make a class you can directly send JSON without Stringifying. Use Default Content Type. Just Use @RequestParam instead of @RequestBody. @RequestParam Name must be same as in json.

Ajax Call

function searchText() {
    var search = {
    "pName" : "bhanu",
    "lName" :"prasad"
    }
    $.ajax({
    type: "POST",
    /*contentType : 'application/json; charset=utf-8',*/ //use Default contentType
    dataType : 'json',
    url: "/gDirecotry/ajax/searchUserProfiles.htm",
    data: search, // Note it is important without stringifying
    success :function(result) {
    // do what ever you want with data
    }
    });

Spring Controller

RequestMapping(value="/gDirecotry/ajax/searchUserProfiles.htm",method=RequestMethod.POST)

   public  @ResponseBody String  getSearchUserProfiles(@RequestParam String pName, @RequestParam String lName) {
       String pNameParameter = pName;
       String lNameParameter = lName;

       // your logic next
   }
like image 42
LAXIT KUMAR Avatar answered Sep 28 '22 05:09

LAXIT KUMAR