I have been trying to get javascript to consume a SOAP service for days and cannot get it to work. It will work in Excel, SOAP-UI, Fiddler, Flex, but not in HTML/Javascript. Any help would be appreciated.
So far I have looked at a simple example at Simplest SOAP example. I followed the code there substituting the appropriate parts for my web service. The errors I get all seem to point to the common CORS errors.
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
I have looked all around to find out why that is the case and tried all sorts of solutions but none have worked.
Here is my code to call the webservice
<script type="text/javascript">
function soap() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open('POST', 'https://myurl', true);
// build SOAP request
var sr =
'<?xml version="1.0" encoding="UTF-8"?>' +
'<soap:Envelope ' +
'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" ' +
'xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" ' +
'xsi:schemalocation="http://schemas.xmlsoap.org/soap/envelope/">' +
'<soap:Body>' +
'<mymethod xmlns="http://my.com/server/">' +
'<Key>123456789</Key>' +
'</mymethod>' +
'</soap:Body></soap:Envelope>';
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
alert('done. use firebug/console to see network response');
}
}
}
// Send the POST request
xmlhttp.setRequestHeader('Content-Type', 'text/xml');
xmlhttp.setRequestHeader('Authorization', 'Basic myusername/password here');
xmlhttp.send(sr);
}
</script>
The HTML is just a button to run the javascript. When I click it I get two errors:
OPTIONS xxxxxxxxxxxxxx 403 (Forbidden)
(xxxxxxxxxxxxx is my url) and
https://xxxxxxxxxxxxxxxxxxxxx: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 403.
Any ideas why this wouldn't be working? Another big questions is why does it work in Fiddler and not on my web page? I used Fiddlers composer tab and entered in pretty much the same information detailed above and it works fine.
Thanks.