Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics NAV forum
Unanswered

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.

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,552 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,552 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans