如何将 Json 对象从 ajax 传递到 spring mvc 控制器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20245544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:22:42  来源:igfitidea点击:

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

ajaxspring-mvc

提问by user2963481

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

我正在使用 SpringMVC,我正在将数据从 ajax 传递到控制器,但我的控制器中出现空值,请检查下面的代码

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

任何人帮助我

回答by Vijay Shegokar

Hey enjoy the following code.

嘿享受以下代码。

Javascript AJAX call

Javascript AJAX 调用

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.

此类的优点是,将来您可以根据需要向此类添加更多变量。
例如。如果你想实现排序功能。

回答by LAXIT KUMAR

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

如果您不想创建一个可以直接发送 JSON 而无需 Stringifying 的类,请使用此方法。使用默认内容类型。只需使用@RequestParam而不是@RequestBody. @RequestParam名称必须与中相同json

Ajax Call

Ajax 调用

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
   }

回答by Tech Mahesh

I hope, You need to include the dataType option,

我希望,您需要包含 dataType 选项,

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
       } 

回答by Sam Kaz

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

如果您可以设法在一个查询字符串参数中传递整个 json,那么您可以在服务器端使用 rest 模板从 json 生成对象,但仍然不是最佳方法

回答by nasarreddy

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