如何从ajax中获得Controller返回的模型的值?
问题描述:
我是一名初学者,所以我提出了很多问题。我希望你明白。如何从ajax中获得Controller返回的模型的值?
AdminController.java
@RequestMapping(value="show_allMember.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.POST)
public @ResponseBody String memberList(ModelAndView mav)
{
List<HashMap<String, Object>> members = aService.getMemberList();
mav.addObject("mList",members);
System.out.println("model is that : " + mav);
return "" + mav ;
}
adminMainPage.jsp
function show_allMember()
{
$('.member_management_area').css('display','block');
$('.member_management').css('color','#fe4978');
$.ajax
({
type: 'POST',
url: 'http://localhost:8080/rachelivf/show_allMember.do',
dataType:'JSON',
success: function(mList)
{
console.log(mList);
$.each(response.mList, function(index, value)
{
console.log(response.list);
});
},
error: function(xhr,ajaxOptions,thrownError)
{
if(xhr.status == 404)
{
alert(thrownError);
}
}
});
}
我试图找到路。但它失败了。
我想从控制器返回的模型的值在 ajax中并将其显示在jsp中。
但我不知道如何。
作为初学者,我不太了解,所以我问了很多问题。 如果我提供了正确的信息,请让我知道。 请让我知道,如果问题的要点是错误的。 我需要你的意见。
答
更改return type
到List<HashMap<String, Object>>
代替String
并返回members
,
@RequestMapping(value="show_allMember.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE,method = RequestMethod.GET)
public @ResponseBody String memberList(ModelAndView mav)
{
List<HashMap<String, Object>> members = aService.getMemberList();
return members ;
//OR
//return aService.getMemberList();
}
在阿贾克斯成功尝试访问作为
type: 'GET',
success: function(mList)
{
console.log(mList);
$.each(response.mList, function(index, value)
{
console.log(value.yourMapKey);
});
},
改变method = RequestMethod.POST
到method = RequestMethod.GET
希望这有助于...
答
您不需要使用
GET
从服务器获取数据。您不需要使用
ModelAndView
。它使用ViewResolver
创建html页面 - 它采用字符串视图名称的形式并将其放入模型的表单对象中。
您可以直接从方法返回所需的数据:
@RequestMapping(value="show_allMember.do",produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
public @ResponseBody List<HashMap<String, Object>> memberList()
{
return aService.getMemberList();
}
和修复的要求:在开发商TOOS
$.ajax
({
type: 'GET',
url: 'http://localhost:8080/rachelivf/show_allMember.do',
dataType:'JSON',
success: function(response)
{
$.each(response, function(index, value)
{
console.log(value);
});
},
error: function(xhr,ajaxOptions,thrownError)
{
if(xhr.status == 404)
{
alert(thrownError);
}
}
});
出现错误406()错误TT –
改变的价值' value =“/ show_allMember.do”'add'/' –
你添加了'Jackson'图书馆罐子吗? –