jquery:$.post("url", {
parentId : provinceValue
}, function(data) {
alert('123456');
}, "json");struts2 action:
try {
out = getResponse().getWriter();
JSONArray json = JSONArray.fromObject(list);//list非空
out.print(json.toString());
out.flush();
out.close();
}
并没有用strust2的json插件,直接使用json的jar问题:没有执行回调函数。还不报错! 可能是什么原因?
parentId : provinceValue
}, function(data) {
alert('123456');
}, "json");struts2 action:
try {
out = getResponse().getWriter();
JSONArray json = JSONArray.fromObject(list);//list非空
out.print(json.toString());
out.flush();
out.close();
}
并没有用strust2的json插件,直接使用json的jar问题:没有执行回调函数。还不报错! 可能是什么原因?
在这句后面在加一句 out.print(true);
response.setContentType("application/json;;charset=UTF-8");编码按照你的来
public static void renderJson(final String string, final String... headers) {
render("application/json", string, headers);
}
/**
* 直接输出内容的简便函数.
* eg.
* render("text/plain", "hello", "encoding:GBK");
* render("text/plain", "hello", "no-cache:false");
* render("text/plain", "hello", "encoding:GBK", "no-cache:false");
*
* @param headers 可变的header数组,目前接受的值为"encoding:"或"no-cache:",默认值分别为UTF-8和true.
*/
public static void render(final String contentType, final String content, final String... headers) {
try {
//分析headers参数
String encoding = "UTF-8";
boolean noCache = true;
for (String header : headers) {
String headerName = StringUtils.substringBefore(header, ":");
String headerValue = StringUtils.substringAfter(header, ":"); if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
encoding = headerValue;
} else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
noCache = Boolean.parseBoolean(headerValue);
} else
throw new IllegalArgumentException(headerName + "不是一个合法的header类型");
} HttpServletResponse response = ServletActionContext.getResponse(); //设置headers参数
String fullContentType = contentType + ";charset=" + encoding;
response.setContentType(fullContentType);
if (noCache) {
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
} response.getWriter().write(content); } catch (IOException e) {
logger.error(e.getMessage(), e);
}
}