How to consume the web api basic authentication method using JQuery ? (Part 9)

In the previous post Part 8, we show that how to test the basic authentication web api using postman.
But if we have to consume the basic authentication web api method in Jquery, then we have to add headers attribute in ajax call.

Complete sample code as given below.

    ViewBag.Title = "Index";

<script src="~/Scripts/jquery-1.10.2.js"></script>

<script type="text/javascript">
    function drawTable(data) {
        for (var i = 0; i < data.length; i++) {
    function drawRow(rowData) {
        var row = $("<tr />")
        row.append($("<td>" + rowData.Id + "</td>"));
        row.append($("<td>" + rowData.EmpName + "</td>"));
        row.append($("<td>" + rowData.EmpAddress + "</td>"));
    $(document).ready(function () {
        $("#btnget").on('click', function (e) {
            var username = "Admin";
            var password = "Admin";
                url: "http://localhost:64410/api/Emps_API_/2",
                type: "GET",
                cache: false,
                contentType: "application/json",
                dataType: "json",
                headers: { 'Authorization': 'Basic ' + btoa(username + ':' + password) }
            }).done(function (data) {
                $('#tbdata tbody').empty();
                if (!jQuery.isArray(data)) data = [data];
                $('#btnget').prop('disabled', true);
            }).error(function (xhr, textStatus, errorThrown) {
                alert("Err " + textStatus + "   " + errorThrown);

<table class="table table-bordered table-striped">
        <td>Click Here to Received the Employee</td>
            <input type="button" id="btnget" value="Get" />


<table class="table-bordered table-striped table table-hover" id="EmpDataTable">
        <th>Emp Name</th>


Note : in the above code headers attributes of Jquery to pass the ‘Authorization’: ‘Basic ‘ + btoa(username + ‘:’ + password), which is required to do the basic authentication process.

Here btoa is JavaScript method to convert the string to base-64 code.


3 thoughts on “How to consume the web api basic authentication method using JQuery ? (Part 9)

  1. Gaylord Bamber November 12, 2019 / 6:11 am

    Fantastic blog! Do you have any recommendations for aspiring writers? I’m hoping to start my own site soon but I’m a little lost on everything. Would you advise starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I’m totally overwhelmed .. Any suggestions? Appreciate it!

    • Chandradev November 24, 2019 / 10:25 am

      Yes, you can start on wordpress

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.