Below are the examples of Synchronous and Asynchronous XMLHttpRequest.
Asynchronous request
Synchronous Request
Here,
requestType can be 'GET' or 'POST'
serverUrL is CRM client url
query is oData query
thanks
p.
Asynchronous request
var req = new XMLHttpRequest();
req.open(requestType, serverURL + "/api/data/v8.2/" + query, false);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status === 200) {
var data = JSON.parse(this.response);
fnSuccessCallback(data);
} else {
var error = JSON.parse(this.response).error;
fnErrorCallBack(error.message);
}
}
};
if (data == null) {
req.send();
} else {
req.send(JSON.stringify(data));
}
}
Synchronous Request
var req = new XMLHttpRequest();
req.open(requestType, serverURL + "/api/data/v8.2/" + query, false);
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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
if (data == null) {
req.send();
} else {
req.send(JSON.stringify(data));
}
req.onreadystatechange = function () {
if (this.readyState == 4 /* complete */) {
req.onreadystatechange = null;
if (this.status === 200) {
var data = JSON.parse(this.response);
fnSuccessCallback(data);
} else {
var error = JSON.parse(this.response).error;
fnErrorCallBack(error.message);
}
}
};
}
Here,
requestType can be 'GET' or 'POST'
serverUrL is CRM client url
query is oData query
thanks
p.
What you wrote as a sync request has errors, please verify the code.
ReplyDelete