First of all I am not using node.js.
My website environment is purely Tomcat + SQL Server + HTML/JSP pages.
Secondly I have checked the similar posts and found none of them answers to my question.
Here is what I want to do:
- Write a js file to modulize an authorization check if a certain user is entitled to view a certain webpage.
- There are two parameters to pass into the function: username & webpage name.
- So in each single webpage I could do the authorization check when it's needed.
P.S. when constructing the js function, I've tried several ways incl. factory/prototype/dynamic/parasitic. I believe it's due to my limited programming knowledge, I could not get the desired returned value from the server.
Here below is my case:
In my js file (sisAuthorization.js), it looks like the following:
//-------------------my sisAuthorization.js file Starts here---------------------
// callback example
var clientData = {
myResult: "not set",
serverAuthCheck: function(username, webpage){
$.ajax({
method: "POST",
async: false,
url: "/psd/webpageAuthorization.ajlog",
dataType: 'json',
data: {
"username": username,
"webpage": webpage
}, success: function (data) {
console.log("server answer is " + data.isauthorized);
if(data.isauthorized == "yes"){
this.myResult = "yes";
console.log("client answer is " + this.myResult);
}else{
this.myResult = "no";
}
}
});
}
}
function getLocalInput(username, webpage, callback, callbackObj){
callback.apply(callbackObj,[username, webpage]);
}
//-------------------my sisAuthorization.js file Ends here---------------------
In my html/jsp webpage, inside the script block, I write the following:
<!-------------------my Webpage Starts here--------------------->
<script type="text/javascript">
$(document).ready(function(){
if(!isUndefined(getCookie("kgusername"))){
$("#myUsername").html(getCookie("kgusername"));
}
//**getCookie("kgusername") returns username**
//**"updatewbs.jsp" is webpage name**
getLocalInput (getCookie("kgusername"),"updatewbs.jsp", clientData.serverAuthCheck, clientData);
console.log (clientData.myResult); // **not set**(I'm expecting "yes" here)
});
</script>
<!-------------------my Webpage Ends here--------------------->
Finally in Chrome Console I've got the following:
- sisAuthorization.js:23 server answer is yes
- sisAuthorization.js:26 client answer is yes
- updatewbs.jsp: not set (I'm expecting "yes" here)
Can anyone help???