<!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript"> var baseURL = 'http://localhost:7047/DynamicsNAV70/WS/'; var cur; var SystemServiceURL = baseURL + 'SystemService'; var VendorPageURL; var SoapEnvelopeNS = 'http://schemas.xmlsoap.org/soap/envelope/'; var SystemServiceNS = 'urn:microsoft-dynamics-schemas/nav/system/'; var VendorPageNS = 'urn:microsoft-dynamics-schemas/page/vendor'; <!-- Function to Invoke a NAV WebService and return data from a specific Tag in the responseXML --> function InvokeNavWS(URL, method, nameSpace, returnTag, parameters) { var result = null; try { var xmlhttp; if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } var request = '<Soap:Envelope xmlns:Soap="' + SoapEnvelopeNS + '">' + '<Soap:Body>' + '<' + method + ' xmlns="' + nameSpace + '">' + parameters + '</' + method + '>' + '</Soap:Body>' + '</Soap:Envelope>'; // Use Post and non-async xmlhttp.open('POST', URL, false); xmlhttp.setRequestHeader('Content-type', 'text/xml; charset=utf-8'); xmlhttp.setRequestHeader('Content-length', request.length); xmlhttp.setRequestHeader('SOAPAction', method); // Setup event handler when readystate changes xmlhttp.onreadystatechange = function () { document.writeln("ReadyState - ", xmlhttp.readyState, "<br/>"); if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { document.writeln("Got response 200", "<br/>"); document.writeln("ResponseXML:", xmlhttp.responseXML.xml, "<br/>"); document.writeln("ResponseText:", xmlhttp.responseText, "<br/>"); xmldoc = xmlhttp.responseXML; xmldoc.setProperty('SelectionLanguage', 'XPath'); xmldoc.setProperty('SelectionNamespaces', 'xmlns:tns="' + nameSpace + '"'); result = xmldoc.selectNodes('//tns:' + returnTag); } } } // Send request will return when event has fired with readyState 4 xmlhttp.send(request); } catch (e) { document.writeln("Got error"); document.writeln(e.toString()); } return result; } // Get the Company list function SystemService_Companies() { return InvokeNavWS(SystemServiceURL, 'Companies', SystemServiceNS, 'return_value', ''); } function VendorPage_Read(no) { return InvokeNavWS(VendorPageURL, 'Read', VendorPageNS, 'Vendor', '<No>' + no + '</No>'); } function VendorPage_ReadMultiple(filters) { return InvokeNavWS(VendorPageURL, 'ReadMultiple', VendorPageNS, 'Vendor', filters); } </script> </head> <body> <span>Hello World!</span><br/> <script type="text/javascript"> var companies = SystemService_Companies(); document.writeln('Companies:<br>'); if (companies == null) { document.writeln('Error <br>'); } else { document.writeln('Companies:<br>'); for (var i = 0; i < companies.length; i++) { document.writeln(companies[i].text + '<br>'); } cur = companies[0].text; } if (cur != null) { VendorPageURL = baseURL + encodeURIComponent(cur) + '/Page/DSIVendor'; document.writeln('<br>URL of Vendor Page: ' + VendorPageURL + '<br>'); var VendorABC = VendorPage_Read('ABC'); document.writeln('<br>Name of Vendor ABC: ' + VendorABC[0].childNodes[2].firstChild.nodeValue + '<br>'); document.writeln('<br>Vendors (filtered):<br>'); var Vendors = VendorPage_ReadMultiple( '<filter><Field>Country_Region_Code</Field><Criteria>US</Criteria></filter>' //+ '<filter><Field>Location_Code</Field><Criteria>RED|BLUE</Criteria></filter>' ); for (i = 0; i < Vendors.length; i++) document.writeln(Vendors[i].childNodes[2].firstChild.nodeValue + '<br>'); } else { document.writeln('no current company<br>'); } document.writeln('<br>THE END'); </script> </body> </html>
Hello World! ReadyState - 4 Got response 200 ResponseXML:undefined ResponseText:Company ACompany BCompany C Companies: Error no current company THE END
Comments
You can add this to the code to make it return the IE9 type of document in the response where you set the request header values
The other key, to resolve the Parameter ??? is null error, you MUST make sure all your the case of the URN and SOAPAction are correct. They are case sensitive. Mismatched case will cause this error:
The complete solution for JavaScript is below. It is pretty much the same from Freddy's Bloghttp://blogs.msdn.com/b/freddyk/archive/2010/01/21/connecting-to-nav-web-services-from-javascript.aspx but I have functionalized it a bit more.