I have an old asp.net soap service that I need to keep running for the next few years but I am updating the interface to run via a webapp instead of an old Windows app.
I am trying to get to work and calling a simple method via jQuery AJAX (probably better ways to do it, sure).
I am running into CORS issues. I have access to the IIS server which the service is running. After checking all over the Googles AND reading suggestions on StackOverflow I implemented a few properties to the web.config to no avail.
I have tried setting the web.config settings:
<add name="Access-Control-Allow-Origin" value="*" />
<!-- <add name="Access-Control-Allow-Methods" value="*" />-->
<add name="Access-Control-Allow-Methods" value="GET,POST,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Content-Type, soapaction" />
So far my jQuery and HTML code is:
<input type="text" value="" />
<span>Push go</span>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" crossorigin="anonymous"></script>
<script>
$('span').on('click', function(){
var number = $('input').val();
justGo(number);
});
//--RUNNING INTO CORS ISSUES
function justGo(incomingNumber){
var SoaMessage = '<?xml version="1.0" encoding="utf-8"?>'
+'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+'<soap:Body>'
+'<IsUserCheckedIn xmlns="http://tempuri.org/">'
+ '<VisitorID>' + incomingNumber + '</VisitorID>'
+'</IsUserCheckedIn>'
+'</soap:Body>'
+'</soap:Envelope>';
var url = "http://servicesite.somesite.com/ws/MyService.asmx?op=GetUserInfo";
//$.support.cors = true; //Even with this on it still same error
$.ajax({
// crossorigin: "anonymous",
type: "GET", //also POST
url: url,
//jsonpCallback: "GetUserInfo", //Using SOAP so this doesn't work?
dataType: "xml",
processData: false,
contentType: "text/xml; charset=\"utf-8\"",
success: function (msg) {
console.log("message: " + msg);
},
error: function (msg, xhr) {
console.log("Failed: " + msg.status + ": " + msg.statusText);
}
});
}
</script>
I am running localhost via VisualStudio Code and Live Server. I also tried just tossing a index.html file up onto the same server as the service, AND a server that has the same domain (note that I am using port 80 right now for everything so it is not an HTTPS issue, and the service is only allowed internal to our networks, so it is not public and yes my local machine can resolve the service URL in my browser).
But so far nothing has helped me to hit the call and get a response back.
It just gets caught with the error:
Access to XMLHttpRequest at 'http://servicesite.somesite.com/ws/MyService.asmx?op=GetUserInfo' from origin 'http://MyCallingSite.somesite.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
OR
Access to XMLHttpRequest at 'http://servicesite.somesite.com/ws/MyService.asmx?op=GetUserInfo' from origin 'http://localhost:5501' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Any ideas is super helpful?
I can ask ChatGPT, but that has given me mixed results in the past and most of the time the same answers I have found elsewhere.
UPDATE:
Current headers being passed in the network is:
Response to preflight
means that your requests are triggering a CORS preflight, which your server most likely doesn't deal with correctly or at all - check the browser developer tools to see how your server is responding toOPTIONS
requests - then read up on how CORS worksOPTIONS
inAccess-Control-Allow-Methods
does NOT configure whatever http server you have to handle CORS preflight