问题描述
- 帮我看看下面的ajax代码有什么问题?
-
$(document).ready(function(){
$.ajax({
type:"post",
url:"friendAction!searchUI.action",
data:"<%=request.getParameter("userId") %>",
async:false,
success:function(msg){
alert(msg);
}
});
})
//辅助获取跳转到的页面信息
public ActionForward searchUI(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {//得到好友 Integer userId = Integer.valueOf(request.getParameter("userId")); userList = userService.getResult("from Users where id=?", new Object[]{userId}); return mapping.findForward(null); } 已经为userList设了get和set方法。
${userList.name }?打不出值。
解决方案
ajax请求非异步返回通常返回json类型,然后在success分支里面获取返回的数据。
你是不是想把userList值返回给ajax的回调函数呢?如果是的话,可以这样修改js和action方法,示例代码:
$.ajax({
type:"post",
dataType:"json",
url:"friendAction!searchUI.action",
data:"<%=request.getParameter("userId") %>",
async:false,
success:function(msg){
alert(msg);
}
});
})
Action中将数据转成JSON返回。
response.setContentType("text/plain");// 设置输出为文字流
response.setCharacterEncoding("UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.write(JSON.toJSONString(obj));
out.flush();
out.close();
时间: 2024-10-02 17:04:11