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 NAV (Archived)

NAV 2017 Cors Error

(0) ShareShare
ReportReport
Posted on by 17

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

I have the same question (0)

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 NAV (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans