ajax返回异常处理

ajax返回异常处理

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

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

方式一:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">

<property name="order" value="3" />

<property name="defaultViews">

<list>

 

<bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"/>

</list>

</property>

</bean>

方式二:

<!-- 采用SpringMVC自带的JSON转换工具,支持@ResponseBody注解 -->

<bean

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

<property name="messageConverters">

<list>

<bean

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

</list>

</property>

</bean> 

 

<!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->

<bean id="mappingJacksonHttpMessageConverter"

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

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

</list>

</property>

</bean> 

 

正常时直接用返回数据

错误时用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 "";

}

 

没有更多推荐了,返回首页