function myTrim(s)
{
//去掉前面空格 aaa
var head=s.substring(0,1);
while(head==" ")
{
s=s.replace(" ","");
head=s.substring(0,1);
}
//去掉后面空格
var len=s.length;
var end=s.substring(len-1,len);
while(end==" ")
{
len--;
end=s.substring(len-1,len);
}
s=s.substring(0,len);
return s;
}
{
//去掉前面空格 aaa
var head=s.substring(0,1);
while(head==" ")
{
s=s.replace(" ","");
head=s.substring(0,1);
}
//去掉后面空格
var len=s.length;
var end=s.substring(len-1,len);
while(end==" ")
{
len--;
end=s.substring(len-1,len);
}
s=s.substring(0,len);
return s;
}
java后台接收字符为乱码
request设置编码
jsons = java.net.URLDecoder.decode(java.net.URLDecoder.decode(jsons , "utf-8"), "utf-8");//这个可以解决%与字母的乱码
@RequestMapping("show.do")
public void showCjxxByCjId(HttpServletRequest request,
HttpServletResponse response)throws IOException{
response.setContentType("application/json;charset=utf-8");
response.setHeader("pragma", "no-cache");
response.setHeader("cache-control", "no-cache"); //这三个response 设置响应值为json格式
String cjId =request.getParameter("cjId");
try {
TpCjxx cjxx = tpcjxxService.geTpCjxx(cjId);
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter(){
public boolean apply(Object source, String name, Object value) {
if(name.equals("tpNltjZtxx")) { //将tpNltjZtxx过滤掉 对象转换json对象时对象里面的对象是不能转换成功的,所以必须过滤掉,否则转换不能成功
return true;
} else {
return false;
}
}
});
JSONObject object = JSONObject.fromObject(cjxx,config);//直接将java对象转换json对象
PrintWriter out = response.getWriter();
out.print(object.toString());
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
log.error(e.getMessage());
}
}
public void showCjxxByCjId(HttpServletRequest request,
HttpServletResponse response)throws IOException{
response.setContentType("application/json;charset=utf-8");
response.setHeader("pragma", "no-cache");
response.setHeader("cache-control", "no-cache"); //这三个response 设置响应值为json格式
String cjId =request.getParameter("cjId");
try {
TpCjxx cjxx = tpcjxxService.geTpCjxx(cjId);
JsonConfig config = new JsonConfig();
config.setJsonPropertyFilter(new PropertyFilter(){
public boolean apply(Object source, String name, Object value) {
if(name.equals("tpNltjZtxx")) { //将tpNltjZtxx过滤掉 对象转换json对象时对象里面的对象是不能转换成功的,所以必须过滤掉,否则转换不能成功
return true;
} else {
return false;
}
}
});
JSONObject object = JSONObject.fromObject(cjxx,config);//直接将java对象转换json对象
PrintWriter out = response.getWriter();
out.print(object.toString());
out.flush();
out.close();
} catch (Exception e) {
// TODO: handle exception
log.error(e.getMessage());
}
}