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