站長資訊網
        最全最豐富的資訊網站

        介紹Spring中ajax與后臺傳輸數據的幾種方式

        ajax欄目介紹與后臺傳輸數據的方法

        介紹Spring中ajax與后臺傳輸數據的幾種方式

        推薦(免費):ajax

        最近寫ajax與后臺傳輸數據的時候碰到了一個問題,我想ajax以json的方式把數據傳輸個后臺,后臺用map的形式接收,然后也以map的形式傳回數據。可是一直碰到前臺報(*)(@415 Unsupported media type) 不支持媒體類型錯誤,然后經過查閱資料終于解決了。這里總結下關于ajax與后臺傳輸數據的幾種方式,上面問題的解決方法在本文最后。


        1.把數據放到url中傳遞
        js:
        <code> var id = $("#id").val(); $.ajax({ type: "POST", url: "/IFTree/people/getPeopleById/"+id,//參數放在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") 如果參數名與url定義的一樣注解可以不用定義("id")         System.out.println(id);         Map<String, Object> map = new HashMap<String, Object>();         return map;     } }

        </code></pre>

        2.把數據放到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"));     // 這里得到的都是字符串得轉換成你需要的類型     Map<String, Object> map = new HashMap<String, Object>();     return map; }

        </code>

        3.以json傳輸(就是開頭說的情況)
        js(包含一些常見的ajax參數解釋):
        <code> var id = $("#id").val(); $.ajax({ type: "POST",//請求類型 timeout:10000,  //設置請求超時時間(毫秒) async:ture,//是否為異步請求 cache:false,//是否從瀏覽器緩存中加載請求信息。 url: "/IFTree/people/getPeopleById", contentType: "application/json;charset=UTF-8",//提交的數據類型 data: JSON.stringify({id:id}),//這里是把json轉化為字符串形式 dataType: "json",//返回的數據類型 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請求的正文數據,然后使用默認配置的HttpMessageConverter進行解析,把數據綁定要對象上面,然后再把對象綁定到controllor中的參數上。
        @ResponseBody
        該注解也是一樣的用于將Controller的方法返回的對象,通過的HttpMessageConverter轉換為指定格式后,寫入到Response對象的body數據區。

        Srping mvc .xml(配置轉換器)

        <code>

         <!-- spring MVC提供的適配器 spring默認加載 (如果不修改默認加載的4類轉換器,該bean可不配置)-->  <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">     <property name="messageConverters">         <!-- 該適配器默認加載以下4類轉換器-->         <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轉換器支持的媒體類型-->         </list>     </property> </bean>

        </code>
        ByteArrayHttpMessageConverter: 負責讀取二進制格式的數據和寫出二進制格式的數據;
        StringHttpMessageConverter: 負責讀取字符串格式的數據和寫出二進制格式的數據;
        ResourceHttpMessageConverter:負責讀取資源文件和寫出資源文件數據;
        FormHttpMessageConverter: 負責讀取form提交的數據
        MappingJacksonHttpMessageConverter: 負責讀取和寫入json格式的數據;
        SouceHttpMessageConverter: 負責讀取和寫入 xml 中javax.xml.transform.Source定義的數據;
        Jaxb2RootElementHttpMessageConverter: 負責讀取和寫入xml 標簽格式的數據;
        AtomFeedHttpMessageConverter: 負責讀取和寫入Atom格式的數據;
        RssChannelHttpMessageConverter: 負責讀取和寫入RSS格式的數據;

        項目里面我用到的只有json轉換器,所以要導入關于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中參數也能以實體類的方式接收數據,
        開始一直報(415 Unsupported media type)的錯誤是因為配置文件沒有寫對也沒導入相應的包。
        如果有哪里不足或錯誤的地方望提出,謝謝_

        想了解

        贊(0)
        分享到: 更多 (0)
        網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號
        主站蜘蛛池模板: 国产精品成人h片在线| 热RE99久久精品国产66热| 日韩欧美亚洲国产精品字幕久久久 | 熟女精品视频一区二区三区| 国产福利电影一区二区三区,亚洲国模精品一区 | 国产午夜精品一本在线观看| 久久99热精品| 69国产成人综合久久精品| 永久无码精品三区在线4| 久久久久无码精品国产app| 91亚洲精品自在在线观看| 精品乱人伦一区二区三区| 成人精品视频99在线观看免费 | 一本一本久久aa综合精品| 人妻少妇精品系列| 久久久国产精品| 国产在线精品国自产拍影院| 亚洲精品线路一在线观看| 久久福利青草精品资源站| 国产精品无码专区| 久久丫精品国产亚洲av| 日韩精品极品视频在线观看免费| 亚洲精品成人片在线观看| 欧美成人精品高清在线播放 | 国产高清在线精品一区| 99在线精品视频观看免费| 精品国产乱码久久久久久1区2区 | 国产精品视频一区二区三区不卡| 91精品无码久久久久久五月天| 青青草国产精品久久久久| 国产精品手机在线| 久久精品国产亚洲一区二区| 99久久夜色精品国产网站| 国产高清在线精品一区二区三区| 中文字幕精品一区二区日本| 国产精品视频色视频| 久久久久国产精品三级网| 日本精品一区二区三区在线视频一| 亚洲av无码成人精品区| 思思久久99热免费精品6| 在线亚洲欧美中文精品|