工作中用到JSON,可当它与Ajax结合使用时有一个槛过不去了.
情况是这样的:
在Server端用Spring MVC往ModelAndView里放进一个由JSONOjbect转来的字符串
String jsonString = JsonUtil.makeJsonStringForMenuUpdate(result, menu, task);
model.put("jsonStringMenuUpdating", jsonString);
return new ModelAndView(???????????,"model",model);
可这个返回路径不好处理了.在不用JSON时,这个路径指向一个jsp,在这个jsp里把Model里的值转为xml:
用如下的代码:
<result>
<return_code><c:out value="${model.result}"/></return_code>
<message><c:out value="${model.cMessage}"/></message>
<nodeValue><c:out value="${model.NodeValue}"/></nodeValue>
<order><c:out value="${model.order}"/></order>
<startDate><fmt:formatDate value="${model.startDate}" pattern="MM/dd/yyyy HH:mm:ss"/></startDate>
<endDate><fmt:formatDate value="${model.endDate}" pattern="MM/dd/yyyy HH:mm:ss"/></endDate>
<task><c:out value = "${model.task}" /></task>
</result>
再在jQuery里用如下的方式来处理这个xml:
type:'post',
datatype:'xml',
success: function(xml) {}.
但把原来的这个xml换成JSON来实现时,从Spring那边返回的JSONString在JavaScript这边怎么接住呢?
Google后,发现jQuery里的ajax可以支持datatype为json,但这个json格式的数据在SpringMVC那端又怎么发出来呢?