Bonjour,
Je souhaiterais via une requête ajax, aller récupérer une List ou une Map et mettre à jour un select. Le tout se trouve dans un modal et la requête est lancé au click du bouton ouvrant cette modal. Je me suis servi de l'exemple suivant : http://programming-tips.wikispaces.c...g+MVC+%26+JSON
J'ai donc ajouter les lib suivantes :
jackson-core-lgpl-1.9.1.jar
jackson-mapper-lgpl-1.9.1.jar
mon dispatcher-servlet.xml
Mon controller
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"> <property name="detectHandlerMethodsInAncestorContexts" value="true" /> </bean> <!-- Configures the @Controller programming model --> <mvc:annotation-driven /> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- json --> <bean id="messageAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> </list> </property> </bean> </beans>
Ma requête :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 @RequestMapping(value="/ajax/modal-film-add-genre") public @ResponseBody Map<String, Object> getRegionSettings(@RequestBody Map<String, Object> obj) { Map<String, Object> json = new HashMap<String, Object>(); json.put("message", "Hello " + obj.get("name") + "!"); return json; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10 $('#btnFilmAddGenre').click(function() { $.ajax({"type": "POST", "contentType": "application/json; charset=utf-8", "url": host+"/ajax/modal-film-add-genre.html", "data": JSON.stringify({"name": "Ricardo"}), "dataType": "json", "success": function(resp) {alert(resp.message);} }); });
Mais je reçois l'erreur 406 ...
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 Réponsevoir le code source Content-Language fr Content-Length 1110 Content-Type text/html;charset=utf-8 Date Mon, 17 Mar 2014 14:38:54 GMT Server Apache-Coyote/1.1 Requêtevoir le code source Accept application/json, text/javascript, */*; q=0.01 Accept-Encoding gzip, deflate Accept-Language fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3 Cache-Control no-cache Connection keep-alive Content-Length 18 Content-Type application/json; charset=utf-8 Cookie JSESSIONID=54455DC2DA81C31E4F64A0426115887A DNT 0 Host localhost:8080 Pragma no-cache Referer http://localhost:8080/IodosProject/film/17413/11:11,%20le%20mal%20a%20un%20nouveau%20num%C3%A9ro.html User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0 X-Requested-With XMLHttpRequest
Auriez vous une idée du problèmes ?
ps : Si j'accède directement à l'url, j'ai bien accès au json
Merci d'avance pour votre aide.
Code : Sélectionner tout - Visualiser dans une fenêtre à part {"message":"Hello "}
Partager