我正在尝试从我的React Web应用程序向Dynamics NAV 2017发出SOAP请求,但出现以下错误:
1)选项http:// ... 401(未授权)
2)CORS策略已阻止从来源“ http://localhost:3000”访问“ http:...”处的XMLHttpRequest:对预检请求的响应未通过访问控制检查:否'Access-Control- Allow-Origin标头出现在所请求的资源上。
这是我的代码:
let soap = () => {
var result = null;
var urlsoap = 'http://schemas.xmlsoap.org/soap/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('Access-Control-Allow-Origin','Content-Type');
xmlhttp.setRequestHeader('Access-Control-Allow-Origin','GET,PUT,POST,DELETE');
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();
这是我的第一个肥皂请求,因此语法可能是错误的。我已经尝试过'easy-soap-request'软件包,但是遇到了同样的错误。任何帮助将不胜感激。
答案 0 :(得分:1)
不能直接为NAV WebService设置CORS标头,因此我们最终尝试的解决方案是使用node.js作为代理服务器并在此处设置cors标头。实际上效果很好。