Hi Experts,
I'm new to WebAPI and javascript (my life has been .Net until now) so I know this is probably a basic question.
My requirement is to query an entity and use that information further in my javascript. I'm having troubles getting the queried data out of the function so that I can use it. My alert from within the function in the onreadystatechange shows that the data was retrieved. My alert that is after the req.send throws an error. How do I get the data outside of that "inner" function so that I can be used by other processes?
Diane
function getContractChange(ccId) {
ccId = "67457cab-aab9-e711-80ea-0050569b0dd7";
var spirit_name
var ccServices = null;
var queryPath = "/api/data/v8.2/spirit_changecontracts(67457cab-aab9-e711-80ea-0050569b0dd7)" +
"?$select=spirit_changecontractnumber,_spirit_contract_value,spirit_contractchangenumber,spirit_generaltypeofchange,spirit_name";
var requestPath = Xrm.Page.context.getClientUrl() + queryPath;
alert(requestPath);
var req = new XMLHttpRequest();
req.open("GET", requestPath, true);
req.setRequestHeader("OData-MaxVersion", "4.0");
req.setRequestHeader("OData-Version", "4.0");
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.onreadystatechange = function () {
if (this.readyState === 4) {
req.onreadystatechange = null;
if (this.status === 200) {
var result = JSON.parse(this.response);
spirit_name = result["spirit_name"];
alert("inside " + spirit_name);
}
else {
alert(this.statusText);
}
}
};
req.send();
alert("name is" + spirit_name);
}
*This post is locked for comments
I have the same question (0)