locked
REST endpoint calling types RRS feed

  • Question

  • Hi,

    I am new to REST callings.

    Can you tell me what is difference between below type of calling REST services.

    Below are two ways to create account record using REST, but what is the diference?

    Is one is Ajax and Other is pure Javascript calling? or One is synchronous and other is Async?.

    Please explain why two ways of calling to create a record?

    ======code====================

    function createAccountRecord(Name)
     {
    var Name="Test account from rest";
    alert("createAccountRecord function START");
    var account = new Object();
    account.Name = Name;
    var jsonAccount = window.JSON.stringify(account);
    var ODataPath="******XRMServices/2011/OrganizationData.svc";

    var createAccountReq = new XMLHttpRequest();
    createAccountReq.open("POST", ODataPath + "/AccountSet", true);
    createAccountReq.setRequestHeader("Accept", "application/json");
    createAccountReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    createAccountReq.onreadystatechange = function () {
    createAccountReqCallBack(this);
    };
    createAccountReq.send(jsonAccount);
    alert("createAccountRecord function END");
    }

    function createAccountRecord1(Name)
    {
          var account = new Object(); //=> ACCOUNT
          account.Name= "Test rest";
          var jsonEntity = JSON.stringify(account);
          var serverUrl = Xrm.Page.context.getServerUrl();
          var ODATA_ENDPOINT = "/XRMServices/2011/OrganizationData.svc";
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    datatype: "json",
    url: "******"+ ODATA_ENDPOINT + "/" + 'AccountSet',
    data: jsonEntity,
    beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("Accept", "application/json"); 
            }, 
            success: function (data, textStatus, XmlHttpRequest) { 
                 alert("ok");
            }, 
            error: function (XmlHttpRequest, textStatus, errorThrown) { 
                alert("ajax call failed" + XmlHttpRequest.toString() + errorThrown.toString() +  textStatus.toString()); 
            } 
        });
    }

    Thanks in Advance

    Krishna

    Tuesday, August 6, 2013 6:27 AM

All replies

  • They both use the same endpoint, are both asynchronous, both use JSON, and both use the XmlHttpRequest object to make an ajax call.

    The main difference is that the second one uses the jqeury library, and the first one doesn't


    Microsoft CRM MVP - http://mscrmuk.blogspot.com/ http://www.excitation.co.uk

    Tuesday, August 6, 2013 8:45 AM
    Moderator
  • Hi David,

    Thanks for your reply.

    Can you tell me when we use Jquery library type and when we use other type. Is there any rule/criteria when should we use specific type?

    Do we use Jquery type when we are creating a record from HTML page, and normal type when we are creating record from CRM interface?

    Please let me understand why two types of callings? In general which one we have to use?

    Thanks

    Krishna

    Tuesday, August 6, 2013 11:03 AM