ajax返回异常处理

ajax返回异常处理

ajax发出的请求要考虑接收方式和返回方式一致

@ResponseBody会自己处理乱码问题(前提xml要配置好)

方式一:

 

方式二:

class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />

 

 

class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">

text/html;charset=UTF-8

 

 

正常时直接用返回数据

错误时用responseText获取返回数据

 

//添加保存

function edit(){

if(!check()){

return false;

}

$.ajax({

url:"${path}/myaccount/eidtUser",

type:"post",

cache:false,

async:true,

dataType:"json",

data:$("#operateUserEditForm").serialize(),

success:function(ret){

 

if(ret==''||ret==null){

alert("操作成功!")

window.location.href="${path}/myaccount/getAccount";

}else{

//alert(2);

//alert(ret);

}

 

},

 

error:function(retMsg){

alert(eval("("+retMsg.responseText+")").msg);

 

}

});

}

 

 

 

@RequestMapping(value = "/eidtUser")

@ResponseBody

public String eidtUser(Model model,TbCusUserBeanVo tbCusUserBeanVo, @RequestParam(value="rolesKey") String rolesKey,@RequestParam(value="pz") String[] pz,HttpServletRequest request, HttpSession session) throws Exception {

CusUserBean getCusUser= CASUtil.getCusUser(request, session);

if(tbCusUserBeanVo==null){

return null;

}

tbCusUserBeanVo.setCustomerKey(BigDecimal.valueOf(Long.valueOf(getCusUser.getCustomerKey())));

try{

accountService.editOperateUser(tbCusUserBeanVo, rolesKey, pz);

}catch(Exception e){

 

e.printStackTrace();

return "{msg:'"+e.getMessage()+"'}";

//"{msg:"+e.getMessage()+"}"throw new Exception(e.getMessage()); //new String( e.getMessage().getBytes(),"utf-8");URLEncoder.encode(e.getMessage(),"utf-8")

 

}

return "";

}

 

你可能感兴趣的:(ajax)