先看下这段action代码
[code="java"]public String getDatas() throws Exception {
response.setHeader("Cache-Control", "no-cache");
//程序立即过期
response.setDateHeader("Expires", 0);
//不要让浏览其缓存程序
response.setHeader("Pragma", "no-cache");
response.setCharacterEncoding("UTF-8");
try {
//response.getWriter().print("[{'id':'aa','name':'bb'}]");
//response.getWriter().print("123功");
response.getWriter().print("123");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
response.getWriter().flush();
response.getWriter().close();
}
return null;
}[/code]
这是页面中的
[code="java"]$(document).ready(function(){
$.getJSON("./modul_getDatas",null,function(data){
alert(data);
})
});[/code]
在Action中只有print中返回数字,在前台的回调函数才回提示,其它的都不回提示。网上找了说加上
// response.setContentType("application/json");
// response.setContentType("text/json; charset=utf-8");
可还是没有用,还找到像[url]http://fogandsun.blog.163.com/blog/static/3665887420108152337687/[/url]这个论坛中的。
谁能解释一下为什么换成数字以外的字符就不行了呢?
将你的//response.getWriter().print("[{'id':'aa','name':'bb'}]");
改为response.getWriter().print("[{\"id\":\"aa\",\"name\":\"bb\"}]"); 或者直接使用json类库生成json对象
字母应该也可以吧