I have AJAX using jQuery which gets triggered when I click a link with the ID logOut
.
AJAX:
$("#logOut").click(function(){
$.ajax({
url: "Logout"
});
});
The AJAX calls my Java Servlet, see below:
/**
* Servlet implementation class Logout
*/
@WebServlet(description = "Logout User", urlPatterns = { "/Logout" })
public class Logout extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*//*
public Logout() {
super();
// TODO Auto-generated constructor stub
}*/
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", "index.jsp#login");
}
}
When the link is clicked my jQuery always triggers however it does not always seem to call the logout Java Servlet. Why is this happening?
EDIT:
When I step through the jQuery code using Chrome dev tools it works but not when it's ran normally.
HTML:
<li><a class="linkButtons" href="" id="logOut">Log Out</a></li>