- "display:none">
- "index.html"><<主页"loggedIn"style="display:none">
- 欢迎
- "username">."#"id="logout">登出
- 安全页面
- 安全内容:
- "secureContent" class="secure">
- "text"id="newusername"placeholder="用户名"/>
- "newpassword"type="password"placeholder="密码"/>
- "button"onclick="createUser()"/>"createuserresult">"text/javascript">
- $(document).ready(function () {
- //如果 apiKey 存在, 则表示用户已登录
- //检查 apiKey 是否存在
- varapiToken = ApiToken.Get();
- if (apiToken.IsValid) {
- //用户已登录
- //获取和显示安全内容
- //使用我们存储在 cookie 中的 apiKey
- varrequest = { apiKey: apiToken.Key };
- //成功视图
- varsuccess = function (response) {
- $("#username").html(apiToken.Username);
- $("#secureContent").html(response.SecureContent);
- $("body").show();
- };
- //将用户重定向到401上的登录页
- varerror = function (response) {
- if(response.status ==401) {
- window.location ="login.html";
- return;
- }
- alert("出问题了"+ response.error);
- };
- $.ajax({
- url: api.secure,
- data: request,
- success: success,
- error: error,
- dataType:"json"
- });
- }
- else {
- //如果没有保存 apiToken, 则表示用户尚未登录
- //将 "em 打包" 发送到登录页window.location ="login.html";
- }
- if (apiToken.IsValid) {
- $("#loggedIn").show();
- $("#username").html(apiToken.Username);
- $("#logout").click(function () {
- //将 apiKey 发送到要销毁的 api
- varrequest = { apiKey: apiToken.Key };
- //当契约在服务器上完成时,
- //删除客户端中的 cookie, 然后重定向到主页。
- varsuccess = function () {
- apiToken.Delete();
- window.location ="index.html";
- };
- $.ajax({
- type: 'DELETE',
- url: api.auth,
- data: resquest,
- dataType: "json"
- });
- });
- }
- });
- function createUser() {
- varapiToken = ApiToken.Get();
- if (apiToken.IsValid) {
- //用户已登录
- varrequest = {
- username: $("#newusername").val(),
- password: $("#newpassword").val()
- };
- //成功视图
- varsuccess = function (response) {
- $("#createuserresult").html("用户"+ response.username +"已创建");
- };
- //将用户重定向到401上的登录页
- varerror = function (response) {
- if(response.status ==401) {
- window.location ="login.html";
- return;
- }
- alert("出问题了"+ response.error);
- };
- $.ajax({
- type: "POST",
- url: api.create_user +"?ApiKey="+ apiToken.Key,
- data: request,
- success: success,
- error: error,
- dataType:"json"
- });
- }
- else {
- //如果没有保存 apiToken, 则表示用户尚未登录
- //将 "em 打包" 发送到登录页window.location ="login.html";
- }
- }
来源: http://www.cnblogs.com/R00R/p/6854529.html