[b]1:设置cookie[/b]
$.cookie("name", $("#name").val(),
{
expires : _expires
});
[b]2:jquery提示[/b]
$.messager.alert('警告', '密码不能为空', 'warning');
如果alert的时候出现乱码,解决方法:
前些天还可以正常使用的js文件,在添加了一些东西后,其中的alert()弹出提示框总是中文乱码,在网上看了很多,给出的答案基本上是加一个<meta http-equiv="Content-Type" content="text/html;charset=gb2312" /> 或者是将gb2312换成utf-8,我用多种方法测了很久还是没能解决这问题。 最后终于在某个论坛的一个角落里发现了点东西,原来在html里面引用js文件时要指定字符集的编码方式,具体做法是<script charset="gb2312" language="javascript" type="text/javascript" src="ntkoocx.js"></script>。
这里提供一种解决办法。
在这句语句上面加上echo "<meta http-equiv='Content-Type'' content='text/html; charset=utf-8'>";
注意里面的charset编码需要填写你网页对应的编码。实例如下:
echo "<meta http-equiv='Content-Type'' content='text/html; charset=utf-8'>";
echo "<script charset='utf-8' type='text/javascript'>alert('请登录');window.location.href='admin/index';</script>";
baseAction 如下:
public void returnJSON(HttpServletResponse response, String value)
{
PrintWriter out = null;
try
{
//设置编码
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
// 设置浏览器不要缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
out = response.getWriter();
out.write(value);
out.flush();
}
catch (IOException e)
{
BaseAction.LOGGER.error(e);
}
finally
{
out.close();
}
}
public void returnJSON(HttpServletResponse response, JSONObject jsonObject)
{
returnJSON(response, jsonObject.toString());
}
/**
* @param response response
* @param jsonArray jsonArray
*/
public void returnJSON(HttpServletResponse response, JSONArray jsonArray)
{
if (jsonArray.isEmpty())
{
returnJSON(response, "[]");
}
else
{
returnJSON(response, jsonArray.toString());
}
}
/**
* @param response response
* @param msg msg
*/
public void returnError(HttpServletResponse response, String msg)
{
if (BaseAction.LOGGER.isDebugEnabled())
{
System.out.println(msg);
}
returnJSON(response, "{" + Constants.ERROR_CODE_KEY + ":'" + msg + "'}");
}
/**
* @param response response
* @param msg msg
* @param data data
*/
public void returnError(HttpServletResponse response, String msg, String data)
{
if (BaseAction.LOGGER.isDebugEnabled())
{
System.out.println(msg);
}
returnJSON(response, "{" + Constants.ERROR_CODE_KEY + ":'" + msg + "',data:" + data + "}");
}
/**
* @param response response
* @param msg msg
* @param data data
*/
public void returnError(HttpServletResponse response, String msg, JSONObject data)
{
returnError(response, msg, data.toString());
}
/**
* @param response response
* @param msg msg
* @param data data
*/
public void returnError(HttpServletResponse response, String msg, JSONArray data)
{
returnError(response, msg, data.toString());
}
/**
* @param response response
* @param errorInfo errorInfo
*/
public void returnError(HttpServletResponse response, ErrorInfo errorInfo)
{
returnJSON(response, JSONObject.fromObject(errorInfo));
}
/**
* 获得当前用户
* @param request request
* @return 当前用户
*/
public User getCurrentUser(HttpServletRequest request)
{
User user = (User) request.getSession().getAttribute(Constants.CURRENT_USER);
return user;
}
前台获取错误提示:
function checkError(data)
{
if(!data || data == "")
{
return false;
}
eval("var data = " + data);
if(data && data.errorCode)
{
var msg = data.errorCode;
if(ErrorCode[data.errorCode])
{
msg = ErrorCode[data.errorCode];
}
if(data.errorLevel == "warning")
{
$.messager.alert('警告', msg, 'warning');
}
else
{
$.messager.alert('错误', msg, 'error');
}
return false;
}
return true;
}
$.cookie("name", $("#name").val(),
{
expires : _expires
});
[b]2:jquery提示[/b]
$.messager.alert('警告', '密码不能为空', 'warning');
如果alert的时候出现乱码,解决方法:
前些天还可以正常使用的js文件,在添加了一些东西后,其中的alert()弹出提示框总是中文乱码,在网上看了很多,给出的答案基本上是加一个<meta http-equiv="Content-Type" content="text/html;charset=gb2312" /> 或者是将gb2312换成utf-8,我用多种方法测了很久还是没能解决这问题。 最后终于在某个论坛的一个角落里发现了点东西,原来在html里面引用js文件时要指定字符集的编码方式,具体做法是<script charset="gb2312" language="javascript" type="text/javascript" src="ntkoocx.js"></script>。
这里提供一种解决办法。
在这句语句上面加上echo "<meta http-equiv='Content-Type'' content='text/html; charset=utf-8'>";
注意里面的charset编码需要填写你网页对应的编码。实例如下:
echo "<meta http-equiv='Content-Type'' content='text/html; charset=utf-8'>";
echo "<script charset='utf-8' type='text/javascript'>alert('请登录');window.location.href='admin/index';</script>";
baseAction 如下:
public void returnJSON(HttpServletResponse response, String value)
{
PrintWriter out = null;
try
{
//设置编码
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
// 设置浏览器不要缓存
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
out = response.getWriter();
out.write(value);
out.flush();
}
catch (IOException e)
{
BaseAction.LOGGER.error(e);
}
finally
{
out.close();
}
}
public void returnJSON(HttpServletResponse response, JSONObject jsonObject)
{
returnJSON(response, jsonObject.toString());
}
/**
* @param response response
* @param jsonArray jsonArray
*/
public void returnJSON(HttpServletResponse response, JSONArray jsonArray)
{
if (jsonArray.isEmpty())
{
returnJSON(response, "[]");
}
else
{
returnJSON(response, jsonArray.toString());
}
}
/**
* @param response response
* @param msg msg
*/
public void returnError(HttpServletResponse response, String msg)
{
if (BaseAction.LOGGER.isDebugEnabled())
{
System.out.println(msg);
}
returnJSON(response, "{" + Constants.ERROR_CODE_KEY + ":'" + msg + "'}");
}
/**
* @param response response
* @param msg msg
* @param data data
*/
public void returnError(HttpServletResponse response, String msg, String data)
{
if (BaseAction.LOGGER.isDebugEnabled())
{
System.out.println(msg);
}
returnJSON(response, "{" + Constants.ERROR_CODE_KEY + ":'" + msg + "',data:" + data + "}");
}
/**
* @param response response
* @param msg msg
* @param data data
*/
public void returnError(HttpServletResponse response, String msg, JSONObject data)
{
returnError(response, msg, data.toString());
}
/**
* @param response response
* @param msg msg
* @param data data
*/
public void returnError(HttpServletResponse response, String msg, JSONArray data)
{
returnError(response, msg, data.toString());
}
/**
* @param response response
* @param errorInfo errorInfo
*/
public void returnError(HttpServletResponse response, ErrorInfo errorInfo)
{
returnJSON(response, JSONObject.fromObject(errorInfo));
}
/**
* 获得当前用户
* @param request request
* @return 当前用户
*/
public User getCurrentUser(HttpServletRequest request)
{
User user = (User) request.getSession().getAttribute(Constants.CURRENT_USER);
return user;
}
前台获取错误提示:
function checkError(data)
{
if(!data || data == "")
{
return false;
}
eval("var data = " + data);
if(data && data.errorCode)
{
var msg = data.errorCode;
if(ErrorCode[data.errorCode])
{
msg = ErrorCode[data.errorCode];
}
if(data.errorLevel == "warning")
{
$.messager.alert('警告', msg, 'warning');
}
else
{
$.messager.alert('错误', msg, 'error');
}
return false;
}
return true;
}