function InitiateAPICalls() {
var resp = ExecuteAPIRequest("accounts"); //Ajax Request
var resp1 = ExecuteXmlRequestToAPI("accounts"); //XMLHttpRequest
}
function result(resultArray, error) {
this.results = resultArray;
this.error = error;
}
function ExecuteAPIRequest(query) {
var resultArray = null;
var errorMessage = null;
$.ajax(
{
type: "GET",
url: Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" +query,
async: false,
headers: { "Accept": "application/json", "Content-Type": "application/json;charset=utf-8", "OData-MaxVersion": "4.0", "OData-Version": "4.0" }
})
.done(function (Httpresponse) {
resultArray = Httpresponse.value;
})
.fail(function (Httpresponse) {
var errors = JSON.parse(Httpresponse.responseText);
if (errors.error)
errorMessage = "There was an error executing the request. " + Httpresponse.status + " " + Httpresponse.statusText +". " + errors.error.message;
})
.always(function (Httpresponse) {
});
return new result(resultArray,errorMessage);
}
function ExecuteXmlRequestToAPI(query) {
var resultArray = null;
var errorMessage = null;
var req = new XMLHttpRequest()
var resultArray = null;
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/"+ query, false);
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.onerror = function (error) {
throw new Error("There was an error executing the request. " + req.status + " " + error.message);
}
var response = req.send();
if (req.status == 200) {
var results = JSON.parse(req.response);
resultArray = results.value;
}
else {
var errors = JSON.parse(req.response);
if (errors.error)
errorMessage = "There was an error executing the request. " + req.status + " " +req.statusText+ ". "+ errors.error.message;
}
return new result(resultArray,errorMessage);
}
var resp = ExecuteAPIRequest("accounts"); //Ajax Request
var resp1 = ExecuteXmlRequestToAPI("accounts"); //XMLHttpRequest
}
function result(resultArray, error) {
this.results = resultArray;
this.error = error;
}
function ExecuteAPIRequest(query) {
var resultArray = null;
var errorMessage = null;
$.ajax(
{
type: "GET",
url: Xrm.Page.context.getClientUrl() + "/api/data/v8.0/" +query,
async: false,
headers: { "Accept": "application/json", "Content-Type": "application/json;charset=utf-8", "OData-MaxVersion": "4.0", "OData-Version": "4.0" }
})
.done(function (Httpresponse) {
resultArray = Httpresponse.value;
})
.fail(function (Httpresponse) {
var errors = JSON.parse(Httpresponse.responseText);
if (errors.error)
errorMessage = "There was an error executing the request. " + Httpresponse.status + " " + Httpresponse.statusText +". " + errors.error.message;
})
.always(function (Httpresponse) {
});
return new result(resultArray,errorMessage);
}
function ExecuteXmlRequestToAPI(query) {
var resultArray = null;
var errorMessage = null;
var req = new XMLHttpRequest()
var resultArray = null;
req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/"+ query, false);
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.onerror = function (error) {
throw new Error("There was an error executing the request. " + req.status + " " + error.message);
}
var response = req.send();
if (req.status == 200) {
var results = JSON.parse(req.response);
resultArray = results.value;
}
else {
var errors = JSON.parse(req.response);
if (errors.error)
errorMessage = "There was an error executing the request. " + req.status + " " +req.statusText+ ". "+ errors.error.message;
}
return new result(resultArray,errorMessage);
}
No comments:
Post a Comment