Add Bearer ticket to server side Ajax call Using DataTable

Add Bearer ticket to server side Ajax call Using DataTable

sairampamidi1989sairampamidi1989 Posts: 2Questions: 2Answers: 0

Hello All,

This code i written for calling the web api of crm

var fetchxml = "<fetch mapping='logical' page='2' count='10' returntotalrecordcount='true' pagingcookie='%253ccookie%2520page%253d%25221%2522%253e%253caccountid%2520last%253d%2522%257bB8A19CDD-88DF-E311-B8E5-6C3BE5A8B200%257d%2522%2520first%253d%2522%257b475B158C-541C-E511-80D3-3863BB347BA8%257d%2522%2520%252f%253e%253c%252fcookie%253e'>" +
" <entity name='account' >" +
" <attribute name='address1_city' />" +
" <attribute name='name' />" +
" </entity>" +
"</fetch>";

        $("#example").DataTable({
            "processing": true,
            "serverSide": true,
            "aoColumnDefs": [

{ bSortable: true, aTargets: [0] },
{ bSortable: false, aTargets: ['_all'] }
],
"ajax":
{
url: encodeURI(organizationURI + "/api/data/v9.0/accounts?fetchXml=" + fetchxml),
dataType: 'json',
method: 'get',
headers: { 'Authorization': 'Bearer' + token },
data: function (d) {
return d;
},

                },
            columns: [
    { title: "Name" },
    { title: "City" },

            ]
        });

it is throwing the 401 Un Authroizer error..

i written following code it is execute with out any error,

var fetchxml = "<fetch mapping='logical' page='2' count='10' returntotalrecordcount='true' pagingcookie='%253ccookie%2520page%253d%25221%2522%253e%253caccountid%2520last%253d%2522%257bB8A19CDD-88DF-E311-B8E5-6C3BE5A8B200%257d%2522%2520first%253d%2522%257b475B158C-541C-E511-80D3-3863BB347BA8%257d%2522%2520%252f%253e%253c%252fcookie%253e'>" +
" <entity name='account' >" +
" <attribute name='emailaddress1' />" +
" <attribute name='address1_city' />" +
" <attribute name='accountid' />" +
" <attribute name='parentaccountid' />" +
" <attribute name='address1_composite' />" +
" <attribute name='name' />" +
" </entity>" +
"</fetch>";

              var req = new XMLHttpRequest()
              req.open("GET", encodeURI(organizationURI + "/api/data/v9.0/accounts?fetchXml=" + fetchxml), true);
              //Set Bearer token
              req.setRequestHeader("Authorization", "Bearer " + token);
              req.setRequestHeader("Accept", "application/json");
              req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
              req.setRequestHeader("OData-MaxVersion", "4.0");
              req.setRequestHeader("OData-Version", "4.0");
              req.setRequestHeader("Prefer", "odata.include-annotations=*");
              req.onreadystatechange = function () {
                  if (this.readyState == 4 /* complete */) {
                      req.onreadystatechange = null;
                      if (this.status == 200) {
                          var accounts = JSON.parse(this.response).value;
                          var temp = JSON.parse(this.response);
                          temp["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"];
                          var entityObj = [];
                          var pagingInfo = null;
                          debugger;

                          //Check if results contains cookies
                          //if yes then retrieve next set of records
                          if (temp["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"] != null) {

                              document.getElementById("next").style.display = "block";
                              document.getElementById("nxtPage").textContent = "";
                              document.getElementById("nxtPage").textContent = temp["@Microsoft.Dynamics.CRM.fetchxmlpagingcookie"];
                          }
                          renderAccounts(accounts);
                          var data = [];
                          for (var i = 0; i < accounts.length; i++) {
                              var name = accounts[i].name;
                              var city = accounts[i].address1_city;
                              data.push([name, city]);
                          }

                      }
                      else {
                          var error = JSON.parse(this.response).error;
                          console.log(error.message);
                          errorMessage.textContent = error.message;
                      }
                  }
              };
              req.send();

Thanks in advance,
Sairam

Answers

  • allanallan Posts: 62,994Questions: 1Answers: 10,368 Site admin

    it is throwing the 401 Un Authroizer error..

    What does your API expect that you aren't sending in the Ajax call?

    Allan

This discussion has been closed.