0

api calling from web application ajax (401 unauthorize error). all cookie get in api ,in this cookie access token available but api will not authorize in this code used a withCredentials: true and using identity server 6

web application ajax code below hare

    $(document).ready(function () {
        GetData();
        function GetData() {
            $.ajax({
                type: "GET",
                url: "https://localhost:7129/Customer",
                crossDomain: true,                
                xhrFields: {
                    withCredentials: true
                },              
                dataType: "json",                
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    "Access-Control-Allow-Methods": "*",
                    "Access-Control-Allow-Headers": "*",
                    'Content-Type': 'application/json'
                },
                credentials: 'include',
                success: function (data) 
                {
                    alert(JSON.stringify(data));
                   
                    }); //End of foreach Loop

                }, //End of AJAX Success function

                failure: function (data) {
                    alert( "failure " + data.responseText);
                }, //End of AJAX failure function
                error: function (xhr, status, error) {
                    // Handle the error
                    console.log("AJAX request failed.");
                    console.log("Status: " + status);
                    console.log("Error: " + error);
                } //End of AJAX error function

            });
        }

    });

in this ajax code send access token in header api authorize successful

  headers: {
               "Authorization": "Bearer " + "Access Token",

            },

but cookie base authorize not working getting (error 401 unauthorize)

solving this error 401 unauthorized when calling api from web appllication

1 Answers1

0

I don't know whether this code will help you or not, I am also attaching the link along with the code, hope this might help you https://datatables.net/forums/discussion/45615/how-to-load-table-from-ajax-request

 
    // this is the DataTable initializer  apply the ajax call
    $(".display").DataTable({
        "ajax":function(data, callback, settings) {
 
                // code
 
        }
    });
 
    // this ajax function sends credentials required for authorization
    // and receives the data from the API.
 
     
    $.ajax({
        url: 'http://testdomain/api/testtables',
        method: "GET",
        xhrFields: {
            withCredentials: true
        },
        success: function (data) {
            // loop data to console to verify it is
            // arriving to browser
             
            $.each(data, function(a, b) {
                console.log(b);
            });
        }
    });
     
});```


  [1]: https://datatables.net/forums/discussion/45615/how-to-load-table-from-ajax-request
Sweety SK
  • 351
  • 1
  • 10