使用jquery的ajax提交json串,对输出的json结果进行解析。
jsp页面
<span class="hljs-comment">//请求json,输出是json</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">requestJson</span><span class="hljs-params">()</span>{</span>
$.ajax({
type:<span class="hljs-string">'post'</span>,
url:<span class="hljs-string">'${pageContext.request.contextPath }/requestJson.action'</span>,
contentType:<span class="hljs-string">'application/json;charset=utf-8'</span>,
<span class="hljs-comment">//数据格式是json串,商品信息</span>
data:<span class="hljs-string">'{"</span>name<span class="hljs-string">":"</span>手机<span class="hljs-string">","</span>price<span class="hljs-string">":999}'</span>,
success:<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(data)</span>{//返回<span class="hljs-title">json</span>结果</span>
alert(data);
}
}); }
controller
<span class="hljs-comment">//请求json串(商品信息),输出json(商品信息)</span> <span class="hljs-comment">//@RequestBody将请求的商品信息的json串转成itemsCustom对象</span> <span class="hljs-comment">//@ResponseBody将itemsCustom转成json输出</span> <span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/requestJson"</span>) <span class="hljs-keyword">public</span> @ResponseBody ItemsCustom <span class="hljs-title">requestJson</span>(@RequestBody ItemsCustom itemsCustom){
<span class="hljs-comment">//@ResponseBody将itemsCustom转成json输出</span>
<span class="hljs-keyword">return</span> itemsCustom; }
测试结果
可以看到,request和response的HTTP头的Content-Type都是application/json;charset=utf-8
输入key/value,输出是json串
使用jquery的ajax提交key/value串,对输出的json结果进行解析
jsp页面
<span class="hljs-comment">//请求key/value,输出是json</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">responseJson</span><span class="hljs-params">()</span>{</span>
$.ajax({
type:<span class="hljs-string">'post'</span>,
url:<span class="hljs-string">'${pageContext.request.contextPath }/responseJson.action'</span>,
<span class="hljs-comment">//请求是key/value这里不需要指定contentType,因为默认就 是key/value类型</span>
<span class="hljs-comment">//contentType:'application/json;charset=utf-8',</span>
<span class="hljs-comment">//数据格式是json串,商品信息</span>
data:<span class="hljs-string">'name=手机&price=999'</span>,
success:<span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(data)</span>{</span><span class="hljs-comment">//返回json结果</span>
alert(data.name);
}
}); }
controller
<span class="hljs-comment">//请求key/value,输出json</span> <span class="hljs-annotation">@RequestMapping</span>(<span class="hljs-string">"/responseJson"</span>) <span class="hljs-keyword">public</span> @ResponseBody ItemsCustom <span class="hljs-title">responseJson</span>(ItemsCustom itemsCustom){
<span class="hljs-comment">//@ResponseBody将itemsCustom转成json输出</span>
<span class="hljs-keyword">return</span> itemsCustom; }
测试结果
可以看到,key/value键值对的默认Content-Type是application/x-www-form-urlencoded,同时,我们收到了响应“手机”
作者@brianway更多文章:个人网站 | CSDN | oschina
作者:h3243212 发表于2016/3/14 12:22:27 原文链接
阅读:73 评论:0