ajax Post JSON与SpringMVC
发布时间:2020-12-16 01:47:59 所属栏目:百科 来源:网络整理
导读:1. 配置文件 !-- 处理在类级别上的@RequestMapping注解 -- bean class = "org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" / !-- 处理方法级别上的@RequestMapping注解 -- bean class = "org.springframework.web.ser
1. 配置文件<!-- 处理在类级别上的@RequestMapping注解 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />
<!-- 处理方法级别上的@RequestMapping注解 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="stringConverter" />
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="stringConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<property name="supportedMediaTypes" value="text/html;charset=UTF-8"/>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes" value="application/json" />
</bean>
2. 客户端javaScript代码var orderData = {
bookDate: "2015-11-23",siteType: UcConstant.Dojoy.Site.SITE_TYPE_WHOLE,siteStatus: UcConstant.Dojoy.Site.SITE_STATUS_OCCUPIED,sportType: sportType,siteBookingCarts: [{"siteID": 223,"siteName": "1号场地"}],totalPrice: 0
};
$.ajax({
type: "POST",url: "site/siteOrderDetail",contentType: "application/json",data: JSON.stringify(orderData),//JSON.stringify把object对象转换为json格式的字符串
dataType: "html",success: function (html) {
//handle...
}
});
3. 服务端代码@RequestMapping(method = RequestMethod.POST,value = "/siteOrderDetail",headers = { "Accept=application/json;charset=utf-8" })
public String toSiteOrderDetail(@RequestBody SiteUpdateStatusForm siteOrderForm) {
return "site/site_order_detail.jsp";
}
public class SiteUpdateStatusForm {
private String bookDate;
private int siteStatus;
private int siteType;
private int sportType;
private List<SiteBookingCart> siteBookingCarts;
...
}
public class SiteBookingCart {
private int siteID;
private String siteName;
private int hour;
...
}
完毕。 总结: bookDate=2015-11-23&siteType=0&siteStatus=0&sportType=3&siteBookingCarts%5B0%5D%5BsiteID%5D=338&siteBookingCarts%5B0%5D%5BsiteName%5D=1%E5%8F%B7%E5%9C%BA%E5%9C%B0&siteBookingCarts%5B0%5D%5Bhour%5D=18&siteBookingCarts%5B0%5D%5Bprice%5D=40&siteBookingCarts%5B1%5D%5BsiteID%5D=338&siteBookingCarts%5B1%5D%5BsiteName%5D=1%E5%8F%B7%E5%9C%BA%E5%9C%B0&siteBookingCarts%5B1%5D%5Bhour%5D=19&siteBookingCarts%5B2%5D%5BsiteID%5D=339&siteBookingCarts%5B2%5D%5BsiteName%5D=2%E5%8F%B7%E5%9C%BA%E5%9C%B0&siteBookingCarts%5B2%5D%5Bhour%5D=19&siteBookingCarts%5B3%5D%5BsiteID%5D=339&siteBookingCarts%5B3%5D%5BsiteName%5D=2%E5%8F%B7%E5%9C%BA%E5%9C%B0&siteBookingCarts%5B3%5D%5Bhour%5D=20
那么上述方式会导致服务端解析错误,类似这样: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character ('b' (code 98)): expected a valid value (number,String,array,object,'true','false' or 'null')
如果使用了JSON.stringify,则数据格式是这样的: {"bookDate":"2015-11-23","siteType":0,"siteStatus":0,"sportType":3,"siteBookingCarts":[{"siteID":343,"siteName":"6号场地","hour":19},{"siteID":343,"hour":20},{"siteID":344,"siteName":"7号场地","hour":20}]}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |