Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

undefined is not a constructor evaluating new XMLHttpRequest

Posted on by 350

Hi, for a project with Dynamics 365 Online version 8.2, I wrote a JS script which runs on the Order Entity,  performs a web api query and uses a call to XMLHttpRequest using this pretty straightforward piece of code:

var finalFetchXML = String.Format(query,entityGUID)
var encodedFetchXML = encodeURIComponent(finalFetchXML);
var queryPath = "/api/data/v8.0/salesorderdetails?fetchXml=" + encodedFetchXML;
var requestPath = Xrm.Page.context.getClientUrl() + queryPath;

var req = new XMLHttpRequest();
req.open("GET", requestPath, true);

It works fine in a regular browser but when I use the phone app in iOS (latest version) I found a weird behavior.

  • If I go the any order record and press the refresh option in the phone app I got the following error:

Event: listLoad

Error: undefined is not a constructor (evaluating new XMLHttpRequest())

  • If I never press the refresh option in the phone app, the call is made (asynchronously) and a field is updated in the form (after accepting the save prompts).

I would like to understand the reason behind this behavior with the refresh option and as always, any advise from you is much appreciated.

Xavier

*This post is locked for comments

  • Suggested answer
    Xavier Villafuerte Profile Picture
    Xavier Villafuerte 350 on at
    RE: undefined is not a constructor evaluating new XMLHttpRequest

    Thanks Francesco for your response. It did not work as expected. However I found the solution implementing the call for a new instance of XMLHttpRequest using this code:

    function getXHR(){

    if(window.XMLHttpRequest){

     return new XMLHttpRequest()

    }

    else try {

     return new ActiveXObject('Msxml2.XMLHTTP')

    } catch (e) {

      try{

        return new ActiveXObject("Microsoft.XMLHTTP")

         } catch(e) {

        return null

     }

    }

    }

    Then, I call the getXHR function in my main script as follows:

    var req = getXHR()

    if (req != null) {

    --my code

    }

    I test it with my original scenario (browser and mobile app) and I have no more errors anymore. As a reference I found the code above in this link:

    books.google.com.ec/books;pg=PA151&lpg=PA151&dq=if(XMLHttpRequest)+mobile&source=bl&ots=Va3APYl_5o&sig=S6prjBJarfKrYiUt9nz1iEfd_5o&hl=es&sa=X&ved=0ahUKEwjSnrKthLjYAhVB6SYKHRU3D_UQ6AEIaDAI#v=onepage&q=if(XMLHttpRequest)%20mobile&f=true

    I hope it helps

    Xavier

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: undefined is not a constructor evaluating new XMLHttpRequest

    Hi Xavier,

    if seems the client has not still loaded XMLHttpRequest .

    Try to incapsuate your call in a setInterval method, like this:

    var loadInterval = setInterval(function(){ 

    if(!XMLHttpRequest) return;

    clearInterval(loadInterval);

    // yourcode
    ...

    }, 3000);

    Please let me know if you solve.

    If you found the answer helpful, please mark as Verified 

    Join my network on LinkedIn      Follow me on Twitter 

    Thank You & Best Regards

    Francesco Picchi

    Microsoft Dynamics CRM Consultant, Bologna+Milano, ITALY

    Independent Contractor

    http://www.francescopicchi.com

  • Xavier Villafuerte Profile Picture
    Xavier Villafuerte 350 on at
    RE: undefined is not a constructor evaluating new XMLHttpRequest

    Hi Syed. Well, as far as I understand, the conditional sentence you mention applies only if you want to specify code to be run on an specific type of client, In this case mobile. However, I need to run the web api query in all the Clients and Because of that, I am not using it in my code. Please let me know if I am wrong in any way.

  • Suggested answer
    Syed Ibrahim Profile Picture
    Syed Ibrahim 6,257 on at
    RE: undefined is not a constructor evaluating new XMLHttpRequest

    Hi Xavier,

    Did you include the the below check on the js as first step?

    if (Xrm.Page.context.client.getClient() == "Mobile")

    If not pls add the check and see.

    Hope this helps.If the suggestion resolved your query .Pls mark it as answered/verified to close the thread.

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