ajax欄目介紹與后臺傳輸數(shù)據(jù)的方法
推薦(免費(fèi)):ajax
最近寫ajax與后臺傳輸數(shù)據(jù)的時候碰到了一個問題,我想ajax以json的方式把數(shù)據(jù)傳輸個后臺,后臺用map的形式接收,然后也以map的形式傳回數(shù)據(jù)。可是一直碰到前臺報(*)(@415 Unsupported media type) 不支持媒體類型錯誤,然后經(jīng)過查閱資料終于解決了。這里總結(jié)下關(guān)于ajax與后臺傳輸數(shù)據(jù)的幾種方式,上面問題的解決方法在本文最后。
1.把數(shù)據(jù)放到url中傳遞
js:
<code> var id = $("#id").val(); $.ajax({ type: "POST", url: "/IFTree/people/getPeopleById/"+id,//參數(shù)放在url中 success:function(data){ alert(data); }, error:function(xhr, textStatus, errorThrown) { } }); </code>
后臺:
<pre><code>
@RequestMapping(value = "getPeopleById/{id}") @ResponseBody public Map<String, Object> getPeopleById(@PathVariable("id") int id) { //@PathVariable("id") 如果參數(shù)名與url定義的一樣注解可以不用定義("id") System.out.println(id); Map<String, Object> map = new HashMap<String, Object>(); return map; } }
</code></pre>
2.把數(shù)據(jù)放到data中
js:
<code> var id = $("#id").val(); $.ajax({ type: "POST", url: "/IFTree/people/getPeopleById", data: {id:id}, success:function(data){ alert(data.result); }, error:function(xhr, textStatus, errorThrown) { } }); </code>
后臺(兩個方式):
<pre><code>
@RequestMapping(value = "getPeopleById") @ResponseBody public Map<String, Object> getPeopleById(HttpServletRequest request,HttpServletResponse response) { int id = Integer.valueOf(request.getParameter("id")); Map<String, Object> map = new HashMap<String, Object>(); return map; }
</code></pre>
@RequestMapping(value = "getPeopleById") @ResponseBody public Map<String, Object> getPeopleById(HttpServletRequest request,HttpServletResponse response) { int id = Integer.valueOf(request.getParameter("id")); // 這里得到的都是字符串得轉(zhuǎn)換成你需要的類型 Map<String, Object> map = new HashMap<String, Object>(); return map; }
</code>
3.以json傳輸(就是開頭說的情況)
js(包含一些常見的ajax參數(shù)解釋):
<code> var id = $("#id").val(); $.ajax({ type: "POST",//請求類型 timeout:10000, //設(shè)置請求超時時間(毫秒) async:ture,//是否為異步請求 cache:false,//是否從瀏覽器緩存中加載請求信息。 url: "/IFTree/people/getPeopleById", contentType: "application/json;charset=UTF-8",//提交的數(shù)據(jù)類型 data: JSON.stringify({id:id}),//這里是把json轉(zhuǎn)化為字符串形式 dataType: "json",//返回的數(shù)據(jù)類型 success:function(data){ $("#name").val(data.result.name); }, error:function(xhr, textStatus, errorThrown) { } }); }); </code>
后臺:
<pre><code>
@RequestMapping(value = "getPeopleById", produces = "application/json") @ResponseBody public Map<String, Object> getPeopleById(@RequestBody Map<String, Object> body){ System.out.println(""+body.get("id")); People people = peopleService.getPeopleById(Integer.valueOf((String)body.get("id"))); Map<String, Object> map = new HashMap<String, Object>(); map.put("result", people); return map; }
</code></pre>
詳解:
@RequestBody
該注解首先讀取request請求的正文數(shù)據(jù),然后使用默認(rèn)配置的HttpMessageConverter進(jìn)行解析,把數(shù)據(jù)綁定要對象上面,然后再把對象綁定到controllor中的參數(shù)上。
@ResponseBody
該注解也是一樣的用于將Controller的方法返回的對象,通過的HttpMessageConverter轉(zhuǎn)換為指定格式后,寫入到Response對象的body數(shù)據(jù)區(qū)。
Srping mvc .xml(配置轉(zhuǎn)換器)
<code>
<!-- spring MVC提供的適配器 spring默認(rèn)加載 (如果不修改默認(rèn)加載的4類轉(zhuǎn)換器,該bean可不配置)--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <!-- 該適配器默認(rèn)加載以下4類轉(zhuǎn)換器--> <list> <bean class="org.springframework.http.converter.BufferedImageHttpMessageConverter" /> <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" /> <bean class="org.springframework.http.converter.xml.XmlAwareFormHttpMessageConverter" /> <bean class="org.springframework.http.converter.StringHttpMessageConverter" /> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> </list> </property> </bean><!--這里配置了json轉(zhuǎn)換器支持的媒體類型--> </list> </property> </bean>
</code>
ByteArrayHttpMessageConverter: 負(fù)責(zé)讀取二進(jìn)制格式的數(shù)據(jù)和寫出二進(jìn)制格式的數(shù)據(jù);
StringHttpMessageConverter: 負(fù)責(zé)讀取字符串格式的數(shù)據(jù)和寫出二進(jìn)制格式的數(shù)據(jù);
ResourceHttpMessageConverter:負(fù)責(zé)讀取資源文件和寫出資源文件數(shù)據(jù);
FormHttpMessageConverter: 負(fù)責(zé)讀取form提交的數(shù)據(jù)
MappingJacksonHttpMessageConverter: 負(fù)責(zé)讀取和寫入json格式的數(shù)據(jù);
SouceHttpMessageConverter: 負(fù)責(zé)讀取和寫入 xml 中javax.xml.transform.Source定義的數(shù)據(jù);
Jaxb2RootElementHttpMessageConverter: 負(fù)責(zé)讀取和寫入xml 標(biāo)簽格式的數(shù)據(jù);
AtomFeedHttpMessageConverter: 負(fù)責(zé)讀取和寫入Atom格式的數(shù)據(jù);
RssChannelHttpMessageConverter: 負(fù)責(zé)讀取和寫入RSS格式的數(shù)據(jù);
項目里面我用到的只有json轉(zhuǎn)換器,所以要導(dǎo)入關(guān)于json的包(maven):
<code> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-core-asl</artifactId> <version>1.9.11</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.11</version> </dependency> </code>
同樣controller中參數(shù)也能以實(shí)體類的方式接收數(shù)據(jù),
開始一直報(415 Unsupported media type)的錯誤是因為配置文件沒有寫對也沒導(dǎo)入相應(yīng)的包。
如果有哪里不足或錯誤的地方望提出,謝謝_
想了解