Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

XMLHttpRequest on Form Save

Posted on by Microsoft Employee

I am trying to send a POST request to my Web API on a Entity Form Save. The script is run but my function is not being accessed. I am using a XMLHttpRequest to pass an ID to an API controller. 

function createSiteRequest () {
     var id= Xrm.Page.data.entity.getId();
     var url = "site.cloudapp.net/.../Action" + id;
     var xhr = new XMLHttpRequest();
     xhr.onreadystatechange = function() {
        if (xhr.readyState == XMLHttpRequest.DONE) {
           alert(xhr.responseText);
        }
     }
     xhr.open('POST',url, true);
     xhr.send(null);
}

This seems to be a very basic setup that is working correctly when I use Postman. Does Dynamics restrict requests like this?

Thanks!

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: XMLHttpRequest on Form Save

    Thanks!

  • Suggested answer
    ScottDurow Profile Picture
    ScottDurow 50,177 on at
    RE: XMLHttpRequest on Form Save

    You will need to use a CORS technique to post to an external domain - or if you have access to the webservice you can add the cross domain headers (XMLHttpRequest.setRequestHeader("Access-Control-Allow-Origin", "*.crm.dynamics.com"); - see community.dynamics.com/.../179866

    You could also do the request on the server and call it from an action - see salimadamoncrm.com/.../crm-2013-actions-to-get-around-javascript-cross-domain-challenges

    Hope this helps

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: XMLHttpRequest on Form Save

    It seems like this is a cross domain issue. I implemented a JSONP and CORS solution with no luck either.

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,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans