title: XHR failed loading: POST java spring
body: <p>I am trying to send data from my jsp page to my controller with ajax.
  However i cant get it done and everytime i get the error shown in the title of the post.</p>
  
  <p>JSP content: (This is my modal where i load all my users, i want to add them to a project by sending both my users id and my current projects id to my controller)</p>
  
  <pre><code>&lt;div class="modal fade" id="myModal" role="dialog"&gt;
          &lt;div class="modal-dialog"&gt;
              &lt;!-- Modal content--&gt;
              &lt;div class="modal-content"&gt;
                  &lt;div class="modal-header"&gt;
                      &lt;button type="button" class="close" data-dismiss="modal"&gt;&amp;times;&lt;/button&gt;
                      &lt;h4 class="modal-title"&gt;&lt;/h4&gt;
                  &lt;/div&gt;
                  &lt;div class="modal-body"&gt;
                      &lt;h1 class="text-center"&gt;UserList&lt;/h1&gt;
                      &lt;br&gt;&lt;br&gt;
                      &lt;table class="table table-hover"&gt;
                          &lt;thead&gt;
                              &lt;tr&gt;
                                  &lt;th&gt;Photo&lt;/th&gt;
                                  &lt;th&gt;Firstname&lt;/th&gt;
                                  &lt;th&gt;Lastname&lt;/th&gt;
                                  &lt;th&gt;Function&lt;/th&gt;
                                  &lt;th&gt;Email&lt;/th&gt;
                              &lt;/tr&gt;
                          &lt;/thead&gt;
                          &lt;tbody&gt;
                              &lt;c:forEach var="u" items="${users}"&gt;
                                  &lt;tr&gt;
                                      &lt;td&gt;&lt;img src="${u.getPhoto()}"
                                               alt="Alternate Text" class="img-responsive" /&gt;&lt;/td&gt;
                                      &lt;td&gt;${u.getFirstname()}&lt;/td&gt;
                                      &lt;td&gt;${u.getLastname()}&lt;/td&gt;
                                      &lt;td&gt;${u.getFunction()}&lt;/td&gt;
                                      &lt;td&gt;${u.getEmail()}&lt;/td&gt;
                                      &lt;td&gt;&lt;Button type="Button" onclick="myFunction()" id="addButton" class="btn btn-default"&gt;Add&lt;/button&gt;&lt;/td&gt;
                              &lt;input type="hidden" id="currentProjectId" value="${p.getProjectId()}"&gt;
                              &lt;input type="hidden" id="userId" value="${u.getUserId()}"&gt;
                              &lt;/tr&gt; 
                          &lt;/c:forEach&gt;
                          &lt;/tbody&gt;
                      &lt;/table&gt;
  
                      &lt;script&gt;
                          function myFunction()
                          {
                              var currentProjectId = $('#currentProjectId').val();
                              var userId = $('#userId').val();
  
                              $.ajax({
                                  type: "POST",
                                  contentType: "application/json",
                                  url: "addUserToProject",
                                  data: JSON.stringify({projectId: currentProjectId, userId: userId}),
                                  dataType: 'json',
                                  timeout: 600000,
                                  success: function (data) {
                                      console.log("SUCCESS: ", data);
                                  },
                                  error: function (e) {
                                      console.log("ERROR: ", e);
                                  }
                              });
                          }
                      &lt;/script&gt;
  
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>Controller content:</p>
  
  <pre><code>@RequestMapping(value = "/addUserToProject", method = RequestMethod.POST)
  public @ResponseBody
  String addUser(@RequestParam("projectId") String projectId, @RequestParam("userId") String userId) {
  
      UUID uid = UUID.fromString(userId);
      UUID pid = UUID.fromString(projectId);
      User u = UserList.getInstance().readUserById(uid);
      Project p = ProjectList.getInstance().readProjectByID(pid);
  
      ProjectList.getInstance().addUsertoProject(u, p);
      return "redirect:/projectpage";
  }
  </code></pre>
  
  <p>The result i get now looks like this:
  <a href="https://i.stack.imgur.com/qpTl4.png" rel="nofollow noreferrer">Console output</a></p>
  
  <p>What am i doing wrong here? any help would be very much appreciated!</p>