我正在使用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)
{
}
}
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 :(得分:43)
嘿,请享受以下代码。
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
}
});
}
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
}
以下搜索课程如下
class Search {
private String pName;
private String lName;
// getter and setters for above variables
}
此类的优点在于,将来您可以根据需要向此类添加更多变量。
例如,如果您想实现排序功能。
答案 1 :(得分:6)
如果您不想创建一个类,可以直接发送JSON而不使用Stringifying,请使用此方法。使用默认内容类型。
只需使用@RequestParam
代替@RequestBody
即可。
@RequestParam
名称必须与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
}
答案 2 :(得分:0)
我希望,你需要包含dataType选项,
dataType: "JSON"
您可以在控制器中获取表单数据,如下所示
public @ResponseBody String getSearchUserProfiles(@RequestParam("pName") String pName ,HttpServletRequest request)
{
//here I got null value
}
答案 3 :(得分:0)
如果您可以设法在一个查询字符串参数中传递整个json,那么您可以在服务器端使用rest模板从json生成Object,但仍然不是最佳方法
答案 4 :(得分:-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;
}