Friday 26 March 2021

Call API using ajax in asp.net bearer token bassed

//////////////////////////// Call Token Authontication

function CallAPI_Authoticate(objvr) {

                              var objvr = {
                                    grant_type: 'password',
                                    username: $("[id$=UserName]").val(),
                                    password: $("[id$=Password]").val(),
                                    scope: $('[id$=AnyOtherParameter]').val()
                                };

                        $.ajax({
                            type: "POST",
                            url: "BsaeURL/"+"Token",
                            headers: { "Accept": "application/json" },
                            contentType: "application/x-www-form-url; charset=urf-8",
                            data: objvr,
                            async: false,
                            success: function (result) {                              
                                if (result != "Error")
                                    CallAPI_Authorized(result);
                            },
                            error: function (req, status, error) {
                            
                                    Show_Alert_Msg(JSON.parse(req.responseText).error_description);                            
                            }
                        });
                    }


//////////////////////////// After Authontication API data comming in JSON

function CallAPI_Authorized(token) {                      
                        var tokes = token.access_token;
                        var Baseulr= "http://localhost:60005"
                        var APIURL= "/api/data/GetEmployees";
                        $.ajax({
                            type: "GET",
                            url: "BaseUrl" + "APIURL",
                            async: false,
                            headers: {
                                Authorization: 'Bearer ' + tokes
                            },
                            dataType: 'json',
                            success: function (result, status, xhr) {
                              
                                Other working code here....
                            },
                            error: function (req, status, error) {
                                Show_Alert_Msg(JSON.parse(req.responseText).error_description);
                            }
                        });
                    }

////////////////////////////

[{"districtName":"Haveri","districtCode":"11","talukName":"Haveri","talukCode":"1104","hobliName":"HAVERI","hobliCode":"110401","villageName":"Gowrapura.M.Adura","villageCode":"1104010013","message":"Data Available"}]

No comments:

Post a Comment

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...