日期:2014-05-16 浏览次数:20350 次
function save() { var elemUserinfo = $('#customerInfo'); var userinfo = elemUserinfo.serializeObject(); var jsonuserinfo = JSON.stringify(userinfo); jQuery.ajax({ type : 'POST', contentType : 'application/json', url : 'customerInfo/new.do', data : jsonuserinfo, dataType : 'json', success : function(data){ $('div#responseName').text(data.name); $('div#responseAddr').text(data.addr); }, error : failed }); }
$.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name]) { if (!o[this.name].push) { o[this.name] = [ o[this.name] ]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; };
@Controller @RequestMapping("/customerInfo") public class CustomerInfoController { @RequestMapping(value="/new", method = RequestMethod.POST) @ResponseBody public Customer newCustomer(@RequestBody Customer customer) { customer.setName(customer.getName() + "经服务器修改"); customer.setAddr(customer.getAddr() + "经服务器修改"); return customer; } }
<servlet> <servlet-name>test</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>test</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <util:list id="beanList"> <ref bean="mappingJacksonHttpMessageConverter" /> </util:list> </property> </bean> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />