zl程序教程

您现在的位置是:首页 >  工具

当前栏目

Ajax的工具类AjaxUtils,使用struts返回Json类型详解编程语言

工具AJAXJSONJSON编程语言 使用 详解 类型
2023-06-13 09:20:28 时间
response.setContentType("text/html;charset=utf-8"); response.getWriter().write(text); } catch (IOException e) { e.printStackTrace(); /** *
 

 * 注意:ajax请求必须携带:jsonnp请求callback 

 * eg: 

 * $.getJSON(path + "ajax/news/comment.go?newsId=" + newsId + " content=" +content+ " callback=?", function(data) { 

 * alert(data[val]); 

 * }); 

 * 

*
* @param response
* @param msg
*/
public static void jsonnpResponse(HttpServletResponse response,
String callback, String msg) {
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(callback + "(" + msg + ")");
} catch (IOException e) {

}
}

/**
* 异常操作,需要浏览器地址返回上一步
*
* @param response
* @param alertMsg
* 弹出提示信息
*/
public static void ajaxResponseRollBack(HttpServletResponse response,
String msg) {
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(
" script type=text/javascript alert(" + msg
+ ");window.history.back(); /script
} catch (IOException e) {
// LogConsoleUtil.write(Common.class, "ajaxResponseRollBack()",
// e.getMessage());
}
}

// ajaxJSON返回
public static void ajaxJSONResponse(HttpServletResponse response,
String str, Object object) {
response.setContentType("text/json;charset=utf-8");
JSONObject json = new JSONObject();
json.put(str, object);
try {
response.getWriter().write(json.toString());
} catch (IOException e) {
e.printStackTrace();
}
}

public static void ajaxResponseAlert(HttpServletResponse response,
String msg) {
try {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(
" script type=text/javascript alert(" + msg
+ "); /script
} catch (IOException e) {
// LogConsoleUtil.write(Common.class, "ajaxResponseRollBack()",
// e.getMessage());
}
}

public static Integer StringToInteger(HttpServletRequest request,
String name) {
String obj = request.getParameter(name);
Integer result = -1;
if (obj != null) {
result = Integer.parseInt(obj);
}
return result;
}

}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/10203.html

cgojava