AEM Post Servlet Ajax call fails on first time call when page opened in new browser.
I have a requirement to post a comment on successful authentication and I'm using AEM login mechanism as below:
1. Login using AJAX call
$(document).ready(function() { if(userName && password) { $.ajax({ type:'POST', url : path[0]+"/j_security_check", data: { j_username: userName, j_password: password, j_validate:"true" }, success: function(data, textStatus, jqXHR){ console.log("passed security authentication"); postCommentFunction(); }, error: function(xhr, textStatus) { console.log(textStatus); } }); } else { //error authentication } }2. On successful authentication, the postCommentFunction() which is a javascript function is executed which in turns executes an Ajax call to servlet to post a comment.
function postCommentFunction() { var userName=$(".username").val(); var pageUrl = $(".page-url").val(); var pagetitle = $(".page-title").val(); var comment = $(".comments-area").val(); var operation = "comment"; $.ajax({ url : "/bin/likesAndCommentsNotifier", type : "POST", data : {comment:comment, pageUrl:pageUrl, pageTitle:pagetitle, operation:operation, userName:userName}, success : function(data, textStatus, jqXHR) { console.log("postcomment success"); }, error : function(xhr, status, error){ console.log("postcomment error is:" + xhr.responseText + error); } }); }However, when I first open a new browser and enter the URL and open the page, I click on the login window, enter correct username and password, and on click on submit:
1. The ready function executes the if condition and I see message on console as 'passed security authentication'
2. Then it gives call to postCommentFunction() but executes a error on call to Ajax and returns message as 'postcomment error is: 403 Forbidden. Cannot serve request to /bin/likesAndCommentNotifier in /libs/sling/servlet/errorhandler/default.jsp'
3. When I refresh the page and try again, the postCommentFunction() returns success.
Not sure if this has to do with login token which is created or the Ajax function called inside a JS function called inside a Ajax function.
The case is only when I first open a new browser and try to login the Ajax call inside the function postCommentFunction() fails.
Any pointers will be appreciated greatly.
Thanks,
Jineet