0

I am trying to get client side token authentication working. For some reason my java script does not seem to load in the browser. I have included the script tag.At present the page just hangs and keeps loading forever.

I referred one of the solutions I received for my posts to implement the feature. Implement clientside token-based authentication using plain Javascript/AJAX

Here is my html

*<head runat="server">
    <title></title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"  type="text/javascript"></script>
        <script src="Scripts/lookup.js" type="text/javascript"></script>
    <script src="Scripts/getdatatype.js" type="text/javascript"></script>
</head>
<body>
    <section class="webdesigntuts-workshop">
    <form id="form1" runat="server">
         <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" >

         </asp:ScriptManager>    
    <div>

        <asp:TextBox type="search" name="search" id="sform" placeholder="What are you looking for?" runat="server" />       
         <asp:TextBox type="search" name="hdnText" id="hdnText" runat="server" />

      <button id="Button1" onclick="lookup();" type="button">Search</button>

    </div>
    <div>
        <p> </p>
    </div>

            <div>
                 <button id="Getdtbtn"  type="button">Get Datatypes</button>
            </div>
        </form>
        </section>
</body>
</html>*

Here is my java script.

document.onload=function () {
        var token = isEmpty(window.localStorage.getItem("token"))? gettoken():window.localStorage.getItem("token");

        document.getElementById('Getdtbtn').onclick(function () {
            if (isEmpty(token)) {
                token = gettoken();
            }

            var request = function () {
                $.ajax
             ({
                 type: "GET",
                 url: "http://demo.in-com.com/Xtensions/V4/api/datatypes",
                 data: '{}',
                 beforeSend: function (xhr) {
                     xhr.setRequestHeader('Authorization', window.localStorage.getItem("Basic " + 'token'));
                 },
                 dataType: "json",
                 timeout: 5000,
             });
                request.success(function () {
                    alert('success!');
                });
                request.error(function (httpObj, textStatus) {
                    if (httpObj.status == 401)
                        gettoken();
                        request();
                });
            }
        });
        };

    function gettoken() {
        var credentials = {
            username: "**",
            password: "***",
            domain: "",
            extensionsAppId:"{****}"

        };
        var url = "http://demo.in-com.com/Xtensions/V4/api/login/AuthenticateExternal"
        $.ajax({
            url: url,
            type: 'GET',
            data: { username: credentials.username, password: credentials.password, domain: credentials.domain, extensionsAppId: credentials.extensionsAppId },
            success: function (Data) {
                window.localStorage.setItem('token',  Data.token.split('"').join(''));
            },
            beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', window.localStorage.getItem('token')); },
            error: function () {
                alert('Error occured');
            }
        });

    }

*

Any thoughts on what might be going wrong?

Community
  • 1
  • 1
codingyo
  • 329
  • 1
  • 5
  • 17
  • Do you get any messages in the console? –  Jan 26 '17 at 21:35
  • console does not have any messages either.. – codingyo Jan 26 '17 at 21:37
  • Does the html load? –  Jan 26 '17 at 21:39
  • No.. HTML is not loading. DO you think it could be the html that is corrupted ? – codingyo Jan 26 '17 at 21:41
  • Have you tried testing it without the javascipt? Just to see if the html loads? It might be a problem with your server configuration. –  Jan 26 '17 at 21:42
  • I tried a different browser.. now the html loads.. i can see that my java script has loaded too..however the nothing is happening on page load. – codingyo Jan 26 '17 at 21:44
  • 1
    This is a bad idea, at least the way you are trying to implement it. The `gettoken()` function has the credentials in plain view. Remember that any and all javascript code you have running in the browser is visible to the user if the user is sufficiently curious. – user1429080 Jan 27 '17 at 11:55

0 Answers0