如何将Json对象从ajax传递到Spring MVC控制器?

21

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

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

我的控制器代码

         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
       }

有人能帮帮我吗?


/gDirectory是什么?它是否在WEB-INF文件夹下? - Hosein Aqajani
5个回答

52

嘿,欣赏以下代码。

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控制器代码

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
}

以下是Search类的代码

class Search {
    private String pName;
    private String lName;

    // getter and setters for above variables
}

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


我遇到了问题:Http状态码为405,请求方法Get不受支持。 - Hosein Aqajani
@H.Aqjn 上面的例子是POST请求,你不能在GET请求中传递post数据。 - Vijay Shegokar
1
这段代码救了我,哈哈,继续帮助吧!+1 - J.Doe

8

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

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控制器

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
   }

0

我希望你需要包含dataType选项。

dataType: "JSON"

你可以在控制器中按如下方式获取表单数据:

 public  @ResponseBody String  getSearchUserProfiles(@RequestParam("pName") String pName ,HttpServletRequest request)
       {
         //here I got null value
       } 

Jquery会根据控制器响应的MIME类型推断JSON响应数据,因此不需要指定。 "您从服务器返回的数据类型。如果未指定,则jQuery将尝试根据响应的MIME类型进行推断(XML MIME类型将产生XML,在1.4中,JSON将产生JavaScript对象,在1.4中,脚本将执行脚本,而其他任何内容都将作为字符串返回)。可用的类型(以及作为成功回调的第一个参数传递的结果)是:..." http://api.jquery.com/jquery.ajax/ - Michael Peterson

0
如果您能够将整个JSON作为一个查询字符串参数传递,那么您可以在服务器端使用rest模板从JSON生成对象,但这仍然不是最优的方法。

-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 控制器


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

GET 带有 body 真的吗? - Jose Pose S

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接