19

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

user2963481
  • 1,863
  • 5
  • 17
  • 20

5 Answers5

48

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.

Antonio112009
  • 315
  • 1
  • 4
  • 19
Vijay Shegokar
  • 2,252
  • 1
  • 18
  • 29
7

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
   }
Farside
  • 8,130
  • 2
  • 41
  • 55
LAXIT KUMAR
  • 401
  • 4
  • 7
0

I hope, You need to include the dataType option,

dataType: "JSON"

And you could get the form data in controller as below,

 public  @ResponseBody String  getSearchUserProfiles(@RequestParam("pName") String pName ,HttpServletRequest request)
       {
         //here I got null value
       } 
Tech Mahesh
  • 392
  • 3
  • 14
  • Jquery is going to infer json response data based on the MIME type of the controller response, so it is not required. "The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are: ..." http://api.jquery.com/jquery.ajax/ – Michael Peterson Apr 02 '18 at 12:43
0

If you can manage to pass your whole json in one query string parameter then you can use rest template on the server side to generate Object from json, but still its not the optimal approach

Sam Kaz
  • 192
  • 1
  • 10
-3
u take like this 
var name=$("name").val();
var email=$("email").val();

var obj = 'name='+name+'&email'+email;
  $.ajax({
   url:"simple.form",
   type:"GET",
   data:obj,
   contentType:"application/json",
   success:function(response){
  alert(response);
  },
  error:function(error){
  alert(error);
  }
});

spring Controller


@RequestMapping(value = "simple", method = RequestMethod.GET)
public @ResponseBody String emailcheck(@RequestParam("name") String name, @RequestParam("email") String email, HttpSession session) {

    String meaaseg = "success";
    return meaaseg;
}
Michael Peterson
  • 9,046
  • 3
  • 51
  • 48