使用普通的Javascript

使用普通的Javascript

本文介绍了使用普通的Javascript / AJAX实现基于令牌的客户端身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

有人能指出我使用Javascript解释客户端令牌身份验证实现的文章吗?

Can anyone point me to an article that explains clientside token auth implementation using Javascript?

我发现很多关于Angular的文章,但这不是我想要的。这让我想到了是否可以使用Javascript完成这个问题。

I found many articles on Angular but that is not what I'm looking for. That brings me to the question if it is possible to be done with Javascript.

还有如何处理auth服务器抛出401时的情况。是否存在内置异常检测到这种反应?或者是否需要实现自定义异常?

Also how to handle scenarios when the auth server throws a 401. Is there a built in exception to detect that response? Or is a custom exception required to be implemented?

推荐答案

这对我有用..

var token = gettoken();
function getDatatypes() {
    if (isEmpty(token)) {
        token = gettoken();

    }

    var request = getDatatypesFromApi();
        request.success(function (data) {
            alert('success!');

        });
        request.error(function (httpObj, textStatus) {
            if (httpObj.status == 401)
                gettoken();
        });
}
function getDatatypesFromApi() {
    var request = $.ajax
 ({
     type: "GET",
     url: "http://yoururl.com/",
     data: '',
     headers:{
         'Authorization': 'Basic ' + token
     },
     dataType: "json",
     timeout: 5000,
 });
    return request;
}
function gettoken() {
    var credentials = {
        username: "userid",
        password: "PASS",
        domain: "",
        extensionsAppId:"{extAppId}"

    };
    var url = "http://thelinktoresource/"
    $.ajax({
        url: url,
        type: 'GET',
        data: { userId: credentials.username, password: credentials.password, domain: credentials.domain, extensionsAppId: credentials.extensionsAppId },
        dataType: "json",
        contentType: 'application/json; charset=UTF-8',
        success: function (Data) {
            console.log(Data);
            token = Data.replace(/"/ig, '');
            return token;
        },

        error: function () {
            alert('Error occured');
            return "undefined";
        }
    });

}

function isEmpty(strIn) {
    if (strIn === undefined) {
        return true;
    }
    else if (strIn == null) {
        return true;
    }
    else if (strIn == "") {
        return true;
    }
    else {
        return false;
    }
}

这篇关于使用普通的Javascript / AJAX实现基于令牌的客户端身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 00:03