Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by Microsoft Employee

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

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee 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
    Community Member Microsoft Employee 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans