Error in Ajax call (jQuery) with Thymeleaf

0

I am trying to render a select depending on the selected value in another select.

<form id="myform" action="#" th:action="@{/actionRequest}" th:object="${myModel}" method="post" enctype="multipart/form-data">
        <div class="col-xs-4 div-form-input-bottom">
           <label class="" for="">Select 1:</label>
           <select th:field="*{selectedValue}" class="">
               <option th:each="type : ${source}" th:value="${type.val}" th:text="${type.valor}">val</option>
           </select>
           <span class="error" th:each="err : ${#fields.errors('selectedValue')}" th:text="${err}">error</span>
        </div>
        <div class="">
           <label class="label-form" for="">Select 2:</label>
          <select th:field="*{valueRendered}" class="">
               <option th:each="type : ${sourceRendered}" th:value="${type.val}" th:text="${type.valor}">val</option>
           </select>
        </div>
</form>

The script would be the following

<script th:inline="javascript" type="text/javascript">
/*<![CDATA[*/
var url = /*[[@{/loadDynamic}]]*/;
$(document).ready(function(){
  $("#selectedValue").change(function(){
    var optionSelected = $(this);
    //Sending ajax request
        $.ajax({
            url: url,
            method: 'POST',
            data: {
              optionSelected: optionSelected.val()
            }
        }).done(function (data) {
            $("#valueRendered").html(data);
        }).fail(function (xhr, ajaxOptions, thrownError) {
            console.log("ERROR");
        });
  });
});
/*]]>*/
</script>

The controller is as follows:

@Controller
public class DynamicTestController {

    @RequestMapping(value = "/loadDynamic", method = RequestMethod.POST)
    @ResponseBody
    public String dynamicSelect(MyModel myModel, Model model, @RequestParam("optionSelected") String optionSelected,
    HttpServletRequest request, HttpServletResponse response){
       try {
          /*
           * // Code here
           */
          return "/design/DynamicTest";     
       } catch (Exception e) {
          e.printStackTrace();
       }
       return null;
    }
}

The console throws me the error

  

"NetworkError: 403 - link "

Any ideas on how to solve this?

    
asked by kun 22.05.2017 в 22:13
source

1 answer

0

In case someone serves, I have already solved the problem. Simply disable the 'csrf' in the HttpSecurity configuration.

protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .and()
    .httpBasic()
        .and()
    csrf().disable();}

Greetings

    
answered by 24.05.2017 в 00:55