我正在尝试使客户端令牌身份验证正常工作。出于某种原因,我的 java 脚本似乎没有加载到浏览器中。我已经包含了脚本标签。目前页面只是挂起并一直加载。
我提到了我收到的帖子中的一个解决方案来实现该功能。 使用纯 Javascript/AJAX 实现客户端基于令牌的身份验证
这是我的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>*
这是我的java脚本。
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');
}
});
}
*
对可能出了什么问题有任何想法吗?