Spring Boot thymeleaf Ajax, response works, but doesn't display

Viewed 295

Been trying to understand and search all around the web for an answer, but I just can't seem to get the response to display in the page

This is my fragment which loops on the list with Thymeleaf:

<div id="cnaver" class="col card-deck m1-5 my-3" th:if="${!products.isEmpty()}">
    <div class="carousel-testimony owl-carousel ftco-owl">
        <div class="card border-0" th:each="product : ${products}">
            <div class="item">
                <img class="imgx" th:if="${!product.getImage().isEmpty()}" th:src="${product.getImage()}"/>
                <div class="card-body">
                    <h4 class="card-little" th:text="${product.getName()}"></h4>
                </div>
                <div class="card-footer">
                    <p>
                        <span id="productPriceRtl" th:utext="${product.getPrice()}"></span> עוגיות
                    
                    </p>
                    <a class="btn btn-lg btn-outline-info btn-block spawner" th:href="${product.getName()}">קנה</a>
                    <!-- th:href="${'/spawn/' + product.getName()}" -->
                </div>
            </div>
        </div>
    </div>
</div>

The controller that filters the product list by id given from the ajax

@GetMapping(value = {"/cnav/{ids}"})
public @ResponseBody ModelAndView sendToCNav(@PathVariable("ids") int ids) {
    ModelAndView modelAndView = new ModelAndView("fragments/product::cnavProduct");
    modelAndView.addObject("products", this.productService.getProductListByType(products, ids));
    return modelAndView;
}

And finally the Ajax

$(document).on("click", '.cajax', function() {
    
    $.ajax({
        type: "get",
        url: "/cnav/" + this.id,
        dataType: "html",
        success: function(response) {
            $(response).load(response);
            $("#cnaver").html(response);
        },
        
        error: function(response) {
            alert(response);
        }
    });
});

This is the navigation bar that chooses which url is is sent to the ajax call

        <nav class="cnav" >
    <ul>
      <li class="cselected cajax" id="0">
        <div class="fonta"><i class="fas fa-skull-crossbones fa-3x"></i></div>
      </li>
      <li class="cnotselected cajax" id="1">
        <div class="fonta"><i class="fas fa-gift fa-3x"></i></div>
      </li>
      <li class="cnotselected cajax" id="2">
        <div class="fonta"><i class="fas fa-cat fa-3x"></i></div>
      </li>
      <li class="cnotselected cajax" id="3">
        <div class="fonta"><i class="fas fa-globe-americas fa-3x"></i></div>
      </li>
    </ul>
  </nav>

Now.. the issue is that the response is 100% in html, I'm getting the whole fragment as response, but the page doesn't seem to display anything at all. If for example I change the response to .text(response) instead, I'm able to see the whole fragment as text in the correct place, but the .html(response) doesn't seem to work whatsoever, same with replaceWith and append..

1 Answers
Related