1

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:

  1. Write a js file to modulize an authorization check if a certain user is entitled to view a certain webpage.
  2. There are two parameters to pass into the function: username & webpage name.
  3. 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???

SiHa
  • 7,830
  • 13
  • 34
  • 43
  • @baao Thanks for replying but it is a different question. I have NO problem at all in retrieving data from ajax async call. Pls check the content carefully. – Schumann Yeah Sep 09 '17 at 11:22
  • Yes you have. You are trying to log a value that isn’t set at the time you’re trying to access it. Read the other questions answer carefully and you’ll learn how to use results from an async operation later in your code – baao Sep 09 '17 at 11:24
  • @baao ok. Will do so and will get back to You in case I would still need your help. Thanks again. – Schumann Yeah Sep 09 '17 at 11:26
  • @baao I'd finished checking it an You are so right. It's because of deferred object (the async nature of callback). Now I have fixed My codes. Millions of thanks and have a nice day! – Schumann Yeah Sep 09 '17 at 14:35
  • You too! Nice you got it working! – baao Sep 09 '17 at 18:53

0 Answers0