samedi 5 septembre 2020

Whyt Jquery ajax post does'nt work with HtmlUnit

i am using htmlunit with spring test in order to test all ihm interface from my web application. It works fine with html form (post and get) and with ajax get but i have a problem with ajax post request.

The controller don't received the request. If i replace post by get the junit test case works fine.

this the html view

<html lang="fr" xmlns="http://www.w3.org/1999/xhtml" 
xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Spring Html Unit</title>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://code.jquery.com/jquery-3.5.1.min.js" integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
    <script>
        function postTest() {
            $.ajax({
                type:"post",
                data: {
                    'subject' : 'subject test',
                    'message' : 'message test'
                },
                url:"[[@{/test/post}]]",
                success: function(data, textStatus, jqXHR){
                    $("#result").text(data);
                }
            });
        }       
    </script>
</head>
<body>
    <h4 th:text="'Hello to Thymeleaf '+${myParam}">Hello from Thymeleaf</h4>
    
    <button type="button" onClick="postTest()">Test post</button>
    <div>
        <span>result :</span><span id="result"></span>
    </div>
    
</body>

and the controller

@Controller public class WelcomeController {

@GetMapping("/")
public String init(Model model) {
    model.addAttribute("myParam", "Guillaume");
    return "welcome";
}

@PostMapping("/test/post")
public @ResponseBody String post(@RequestParam String subject, @RequestParam String message, Model model) {
    return subject + " " + message;
}

}

you can also find the complete code on my github https://github.com/guisimon28/spring-test-htmlunit

Can you help me to find if there is some missing configuration or if its a htmlunig bug or pull request ?

Thanks for All

Guillaume

Aucun commentaire:

Enregistrer un commentaire