web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Api rest not working

(0) ShareShare
ReportReport
Posted on by

hello experts,
I am consuming some services in my crm, some if they work but others do not.

For example I am trying to consult the territory service, when doing the get from postman if it works but when I check from javascript does not work.

2146.odata.png

This is the code.

        var retrieveReq = new XMLHttpRequest();
        retrieveReq.open("GET", oDataSelect, false);
        retrieveReq.setRequestHeader("Accept", "application/json");
        retrieveReq.setRequestHeader("Content-Type", "application/json;charset=utf-8");

        retrieveReq.onreadystatechange = function ()
        {
            alert("entro al retriever");

            if (retrieveReq.readyState == 4
                && retrieveReq.status == 200)
            {

                var resultado = JSON.parse(retrieveReq.responseText);
            
            }
        };



the retrieveReq does not bring any data, but the odata does work correctly. any answer would be very useful, thank you very much

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: Api rest not working

    Hi,

    Not sure what is your oDataSelect contains. It will be good to share your full code .

    Try with this-

        var req = new XMLHttpRequest();
        req.open("GET", oDataSelect, 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.onreadystatechange = function () {
            if (this.readyState == 4 /* complete */) {
                req.onreadystatechange = null;
                if (this.status == 200) {
                    var data = JSON.parse(this.response);
                   
                }
                else {
                    var error = JSON.parse(this.response).error;
                    alert(error.message);
                }
            }
        };
        req.send();


  • Suggested answer
    Arun Vinoth Profile Picture
    11,615 Moderator on at
    RE: Api rest not working

    I don’t see any issue with the code except retrieveReq.send() is missing at the last.

    You are making synchronous call by making third variable as false in retrieveReq.open().

    Can you add try catch blocks, debug to see any other errors?

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Api rest not working

    Hi,

    Please use CRM Rest Builder to build the WEB API request. This is the easiest way to build the query as well as execute it and see the results.

    www.toplinestrategies.com/.../crm-rest-builder-useful-tool-working-crm-2016%E2%80%99s-web-api

    However on the first look of your code, i think its the false paramter which you have used it in open method but can't confirm.

    Hope this helps.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Andrés Arias – Community Spotlight

We are honored to recognize Andrés Arias as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
Aric Levin - MVP Profile Picture

Aric Levin - MVP 2 Moderator

#2
MA-04060624-0 Profile Picture

MA-04060624-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans