2013-11-27 50 views
15

我就用SpringMVC的工作,我從阿賈克斯將數據傳遞給控制器​​,但是我在我的控制器空值,請檢查下面如何將json對象從ajax傳遞到spring mvc控制器?

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 
     } 

任何一個可以幫助我

+0

什麼是/ gDirecotry?它是否在'WEB-INF'文件夾下? –

回答

36

嘿享受以下代碼。

的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.htm", 
     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 
    } 

以下搜索類將是如下

class Search { 
    private String pName; 
    private String lName; 

    // getter and setters for above variables 
} 

這個類的好處是,將來你可以在需要的時候向這個類添加更多的變量。
例如。如果你想實現排序功能。

+0

我有問題:HTTP狀態405,請求方法獲取不支持 –

+0

@ H.Aqjn上面的示例是POST請求,您不能在GET請求中傳遞發佈數據。 –

0

我希望你需要包含dataType選項,

dataType: "JSON" 

而且你可以在控制器獲取表單數據如下,

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

如果你能設法通過在一個查詢字符串參數你的整個JSON那麼你可以使用服務器端的其餘模板生成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 
    } 
-2
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 ; 
} 
+0

真正獲得身體嗎? –

相關問題