Skip to main content

Synchronous and asynchronous XMLHttpRequest in Javascript from Microsoft Dynamics CRM perspective

Below are the examples of Synchronous and Asynchronous XMLHttpRequest.

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.

Comments

  1. What you wrote as a sync request has errors, please verify the code.

    ReplyDelete

Post a Comment

Popular posts from this blog

AX 7 - Get record marked and record selected on grid control in X++

Marked Records In AX, we often face a situation where we need to get the reference of those records which are marked on grid control. Using below code you can get those marked records in X++. Records marked can be seen in screenshot on the left. Array markedRecords = gridControl_DS.recordsMarked(); var lastMarkedRecord = markedRecords.lastIndex(); You will get a recId of last marked record in lastMarkedRecord. Using recordMarked() method of grid control data source, you can get an array of marked records which can be used for further iteration based on requirement. Selected Records At the same time, there is another operation called as Selected Records. When a grid control loads, a default first record is selected from the grid. To iterate through selected records, use below code. for (inv = gridControl_ds.getFirst(true) ? gridControl_ds.getFirst(true) : gridControl_ds.cursor(); inv; inv = gridControl_ds.getnext()) {    //logic here } gridControl_...

Mark Customer Transaction from X++ in AX2012

Use below code to mark customers open transaction from X++. static void MarkOpenTransaction() {     custInvoiceJour custInvoiceJour;     SalesTable  salesTable;     specTrans   specTrans;     custTrans   custTrans;     custTransOpen   custTransOpen;     TaxWithholdTrans    taxWithHoldTrans;     TaxWithhold_CustPaym taxWithhold;     CustVendOpenTransManager manager;     ExchangeRateHelper  exch;     AmountCur   totalSettleAmount;     ;     select firstOnly custInvoiceJour where custInvoiceJour.InvoiceId == '004452' ; // && _custInvoiceJour.SalesId == xml_SalesID.text();     if (custInvoiceJour)     {         ttsBegin ;  ...

Call javascript function on sub-grid refresh/load in Microsoft Dynamics CRM

Recently, I came across a requirement where I had to do some calculation based on records added/deleted in sub-grid on CRM form. To achieve this, I have added load event on sub-grid in javascript where I could call the method which is doing the calculations. Below piece of code will be used to add load event on sub-grid and function addEventToGridRefresh() should be registered on form load. function addEventToGridRefresh() { // retrieve the subgrid var grid = Xrm.Page.getControl('#GridControlName#'); // if the subgrid still not available we try again after 2 second if (grid == null) { setTimeout(function () { addEventToGridRefresh(); }, 2000); return; } // add the function to the onRefresh event Xrm.Page.getControl('#GridControlName#').addOnLoad(totalAssetsOfHousehold); // This is used to delay refresh by 1 second because sometimes grid takes time to load. // If you do not include below line, you may not ...