title: 403 Forbidden Error with ajax GET request Spring
body: <p>I am getting a <code>403 forbidden</code>-error every time I try <code>GET</code> a user's information from the database. Relating to my code below, every time I try request by pressing the <strong>Ajax Test</strong> button, It fails to run and gives me an alert, but also in the console gives me a <code>403 Forbidden</code>-error. I am not sure whether it has something to do with Spring security?</p>
  
  <p>Users JSP page:</p>
  
  <pre><code>&lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;User Id&lt;/td&gt;
          &lt;td&gt;Full Name&lt;/td&gt;
          &lt;td&gt;Username&lt;/td&gt;
          &lt;td&gt;Email&lt;/td&gt;
          &lt;td&gt;Date of Birth&lt;/td&gt;
          &lt;td&gt;User Authority&lt;/td&gt;
          &lt;td&gt;Update &lt;/td&gt;
          &lt;td&gt;Delete&lt;/td&gt;
      &lt;/tr&gt;
      &lt;c:forEach var="user" items="${users}"&gt;
          &lt;tr&gt;
              &lt;td&gt;&lt;c:out value="${user.id}" /&gt;&lt;/td&gt;
              &lt;td&gt;&lt;c:out value="${user.name}"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;c:out value="${user.username}"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;c:out value="${user.email}"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;c:out value="${user.dob}"/&gt;&lt;/td&gt;
              &lt;td&gt;&lt;c:out value="${user.authority}"/&gt;&lt;/td&gt;
              &lt;td&gt;
                  &lt;a id="update" href="&lt;c:url value="/viewUser"&gt;&lt;c:param name="id" value="${user.id}"/&gt;&lt;/c:url&gt;"&gt;&lt;button&gt;Update&lt;/button&gt;&lt;/a&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;a id="delete" href="&lt;c:url value="/deleteUser"&gt;&lt;c:param name="id" value="${user.id}"/&gt;&lt;/c:url&gt;"&gt;&lt;button&gt;Delete&lt;/button&gt;&lt;/a&gt;
              &lt;/td&gt;
              &lt;td&gt;
                  &lt;button class="loadUser" name="id" value="${user.id}"&gt;Ajax test&lt;/button&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/c:forEach&gt;
  &lt;/table&gt;
   &lt;div id="personIdResponse"&gt; &lt;/div&gt;
  &lt;script type="text/javascript"&gt;
      $(document).ready(function(){
          $(".loadUser").click(function(e) {
              e.preventDefault();
              var personId = +$(this).val();
              $.get('${pageContext.request.contextPath}/SDP/ajaxTest/' + personId, function(user) {
                    $('#personIdResponse').text(user.name + ', = username ' + user.username);
                  })
              .fail(function(user){
                  alert('Could not load user');
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>User Controller class:</strong></p>
  
  <pre><code>    @RequestMapping("/viewUser")
  public String updateUser(Model model, @RequestParam(value = "id", required = false) Integer id) {
  
      User user = usersService.getUser(id);
  
      model.addAttribute("user", user);
  
      return "settings";
  }
  
  @RequestMapping("/ajaxTest")
  @ResponseBody
  public User ajaxTest(@RequestParam(value = "id", required = false) Integer id) {
  
      User user = usersService.getUser(id); 
      return user;
  }
  </code></pre>