Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV (Archived)

NAV 2017 Cors Error

Posted on by 15

I'm trying to make a SOAP request from my react web application to Dynamics NAV 2017 and I'm getting these errors:

1) OPTIONS http://... 401 (Unauthorized)

2) Access to XMLHttpRequest at 'http:...' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Here is my code:

let soap = () => {
        var result = null;
        var urlsoap = 'schemas.xmlsoap.org/.../envelope'; 

        try { 
            var xmlhttp; 
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest(); 
            } 

            let request = `
            <soapenv:Envelope xmlns:Soap=${urlsoap}> 
                <soapenv:header>
                <soapenv:Body>
                    <ValidateUser>
                        <userName>SOMEUSER</userName>
                    </ValidateUser>
                </soapenv:Body>
                </soapenv:header>
            </soapenv:Envelope>`;



            // Setup event handler when readystate changes 
            xmlhttp.onreadystatechange = function() { 
                if (xmlhttp.readyState === 4) { 
                    if (xmlhttp.status === 200) { 
                        var xmldoc = xmlhttp.responseXML; 
                        xmldoc.setProperty('SelectionLanguage', 'XPath'); 
                        xmldoc.setProperty('SelectionNamespaces', 'xmlns:tns="' + 'urn:microsoft-dynamics-schemas/codeunit/...' + '"'); 
                        result = xmldoc.getElementsByTagName('ValidateUser_Result');
                    } 
                } 
            }


            xmlhttp.open('POST', URL, true);  
            xmlhttp.setRequestHeader('SOAPAction', 'POST');
            xmlhttp.setRequestHeader('Access-Control-Allow-Origin','*');
            xmlhttp.setRequestHeader("Authorization", "Basic " + btoa(myusername:mypassword));
            xmlhttp.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
            xmlhttp.withCredentials = true;
            xmlhttp.send(request); 

        } 

        catch (e) { 
            console.log(e);
        } 

        return result; 
    }

    soap();


Any help would be much appreciated.

*This post is locked for comments

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