【问题标题】:How to pass Json object from ajax to spring mvc controller?如何将 Json 对象从 ajax 传递到 spring mvc 控制器?
【发布时间】:2013-12-13 06:38:02
【问题描述】:

我正在开发 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
       }

谁能帮帮我

【问题讨论】:

  • /gDirecotry 是什么?是在WEB-INF 文件夹下吗?

标签: ajax spring-mvc


【解决方案1】:

我希望,您需要包含 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 中,脚本将执行脚本,其他任何内容都将作为字符串返回)。可用类型(以及作为第一个参数传递给成功回调的结果)是:..." api.jquery.com/jquery.ajax
【解决方案2】:

嘿,喜欢下面的代码。

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

弹簧控制器代码

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不支持
  • @H.Aqjn 上面的例子是POST请求,不能在GET请求中传递post数据。
  • thisss 代码救了我嘿嘿继续帮助 +1
【解决方案3】:

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

【讨论】:

    【解决方案4】:

    如果你不想创建一个类,可以使用这种方法直接发送 JSON 而无需 Stringifying。使用默认内容类型。 只需使用@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
        }
        });
    

    弹簧控制器

    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
       }
    

    【讨论】:

      【解决方案5】:
      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);
        }
      });
      

      弹簧控制器


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

      【讨论】:

      • 真的用身体得到吗?
      猜你喜欢
      • 2015-11-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-03
      • 2013-08-17
      • 2018-05-02
      • 2011-09-05
      • 2014-11-21
      相关资源
      最近更新 更多