Bonjour,
J'ai un projet Spring qui me permet de faire de l'authentification. Avec un url rest et du mapping.
Et au final je "génère" un fichier xml a travers une page jsp. qui sera parse en AJAX par la suite. Seulement le fichier genere ne contient pas d'entete (du type application/xml) car le parser AJAX ne peut le parser.
Voici le code du jsp :
Voici ce que je met dans la methode d'authentification du controller spring :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <?xml version="1.0" encoding="utf-8" ?> <%@ page contentType="application/xml" session="false" pageEncoding="utf-8" %> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/core" %> <response> <errorCode>${errorCode}</errorCode> <message>${message}</message> </response>
Et voila comment je parse le fichier en Ajax
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
33
34
35
36
37
38
39
40 @RequestMapping(value = "/authent/{identity}", method = { RequestMethod.GET, RequestMethod.POST }) public String doAuthent(@PathVariable("identity") String identity, @RequestParam(value="login",required=false,defaultValue="test") String login, @RequestParam(value="pass",required=false,defaultValue="test") String pass, Model model) { dao = new DefaultAccountDao(); Account current = null; try { current = dao.findByIdentity(login); } catch (Exception e) { e.printStackTrace(); } int error = 0; String message = ""; if(current == null) { error = 1; message = "Unable to connect"; model.addAttribute("contentType", "application/xml"); model.addAttribute("errorCode", error); model.addAttribute("message", message); } else { if (current.getCredential().equals(pass)) { error = 0; message = "Authentification success"; model.addAttribute("contentType", "application/xml"); model.addAttribute("errorCode", error); model.addAttribute("message", message); } else { error = 2; message = "Authentification failed"; model.addAttribute("contentType", "application/xml"); model.addAttribute("errorCode", error); model.addAttribute("message", message); } } return "authent_script_response"; }
Je tiens à préciser que je n'ai jamais aligner une ligne d'ajax de ma vie. Ce code Ajax on me la fournit. Il se peut qu'il y ait des problèmes dedans.
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 function onSubmit() { var sf = { login:$("#loginInput").val(), pass:$("#passwordInput").val() }; $.ajax({ url:"http://localhost:8080/isbackend/rest/authent/test", data : sf, dataType : "xml", /** * Ajax process complete. * This method will be called even if the operations * failed. * * @param xhr : the xmlHttpRequest object * @param textStatus : the text associate with the status * @return void */ complete : function(xhr, textStatus) { }, /** * response for a successful process was received, * so, have to redirect to the right page. * * @param data : data sent back by the server * @param textStatus : the text associate with the status * @param xhr : the xmlHttpRequest object * @return void */ success : function(data, textStatus, xhr) { alert(xhr.getAllResponseHeaders()); if($(data).find('errorCode').text() != '0' ){ alert($(data).find('errorCode').text()); }else{ alert('it works'); } }, /** * Error handler. This were called when a response * with a bad HTTP code was sent back or when an error * occurred during response parsing (Jquery). * * Example : code 500 * * @param xhr : the xmlHttpRequest object * @param textStatus : the text associate with the status * @param erroThrown : The text of the error associate the the * @return void */ error : function(xhr, textStatus, errorThrown) { alert(xhr.responseText + ' erreur ' + textStatus); } }); }
J'ai loupé quelque chose? Il y a une manip a faire?
Merci
Partager