ajax return exception handling

ajax return exception handling

The request sent by ajax should be considered consistent with the receiving method and the return method

@ResponseBody will handle the garbled problem by itself (the premise is that the xml must be configured)

method one:

<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>

Method two:

<!-- Using SpringMVC's own JSON conversion tool, support @ResponseBody annotation -->

<bean

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

<property name="messageConverters">

<list>

<bean

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

</list>

</property>

</bean> 

 

<!-- Avoid downloading files when returning JSON when IE executes AJAX -->

<bean id="mappingJacksonHttpMessageConverter"

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

<property name="supportedMediaTypes">

<list>

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

</list>

</property>

</bean> 

 

Use the return data directly when normal

Use responseText to get the return data when there is an error

 

// add save

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("Operation successful!")

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 "";

}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326482202&siteId=291194637