springmvc:配置
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="jsonHttpMessageConverter" /> </list> </property> </bean> <bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
Controller:返回一个自定义的json:只有message和success属性
@RequestMapping("saveClass") @ResponseBody public Object saveClass(Classes classes){ System.out.println(classes.getClassName()); classesDao.save(classes); MyJson mj=new MyJson(); mj.setMessage("添加成功"); mj.setSuccess(true); return mj; }
function save(){ $("#fm").form({ url:"classes/saveClass", success:function(result){ alert(result);//{‘message’:'成功','success':true} alert(res.message);//undefined alert(res['message'])//undefined if(result.success){ $.messager.show({ title:'Info', msg:result.msg, showType:'fade', style:{ right:'', bottom:'' } }); $('#dlg').dialog('close'); // close the dialog $('#dg').datagrid('reload'); }else { $.messager.show({ title: 'Error', msg: result.msg }); } } }); $("#fm").submit(); }
在chrome开发者工具response也是有值的,用result.message取,用result['message']取都是undefined,那出问题了????
var result=eval('('+result+')');解释:http://www.111cn.net/wy/js-ajax/58828.htm