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)

BAD REQUEST When making a AJAX CALL: This.readyState is undefined

(0) ShareShare
ReportReport
Posted on by

I wrote a function that fetches the guid of a lookup field and uses that to make a Web API call. This is the the call that I made:

fetchOptionSet: function (executionContext) {
        var formContext = executionContext.getFormContext(); //get form context
        var client = Xrm.Page.context.getClientUrl(); //get client url
        var childId = formContext.getAttribute("new_childid").getValue()[0].id;
        var child = childId.replace(/[{}]/g, "");

        var contract;
        var req = new XMLHttpRequest();
        req.open("GET", client + `/api/data/v8.2/new_childallergieses(${child})?$select=_new_childid_value`, 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.setRequestHeader("Prefer", "odata.include-annotations=\"*\"");
        req.onreadystatechange = function () {
            if (this.readyState === 4) {
                req.onreadystatechange = null;
                if (this.status === 200) {
                    var result = JSON.parse(this.response);
                    var _new_childid_value = result["_new_childid_value"];
                    contract = _new_childid_value.replace(/[{}]/g, "");
                } else {
                    Xrm.Utility.alertDialog(this.statusText);
                }
            }
        };
        req.send();


However, I get a bad request every time that the script runs. I need the guid returned by the call (contractid) to make another AJAX call! The url is rendered fine, but this.readyState is undefined!

6862.debug.png



*This post is locked for comments

I have the same question (0)
  • Verified answer
    Community Member Profile Picture
    on at
    RE: BAD REQUEST When making a Web API CALL: This.ready is undefined

    I changed the process from Asychronous to Sychronous and it worked, why that happens? No idea...

    var path_one = Xrm.Page.context.getClientUrl() + "/api/data/v8.2/new_childallergieses(" + child + ")?$select=_new_childid_value";

    req.open("GET", path_one , false);

  • Community Member Profile Picture
    on at
    RE: Use @odata.nextLink to query resulting related records?

    dude you already have it there, just get it from the loop!

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
Community Member Profile Picture

Community Member 2

#2
Christoph Pock Profile Picture

Christoph Pock 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans