I am trying to call a C# WCF SOAP Web service from JQuery.
The service is hosted on IIS on port 80, client is run from Apache on port 81, both from localhost. So I fall into cross-origin requests.
Using Chrome, I get expected results, but looking at the network traffic, it shows that the
OPTIONS
request returns error400 Bad Request
, However the nextPOST
request succeeds :Using Firefox, error is raised (JavaScript alert shows
null | error |
). It seems thePOST
request is not sent because theOPTIONS
request failed:Using IE, all is working fine, as if it was from the same origin... I don't see any
OPTIONS
request:
The exposed function from the interface :
namespace Test
{
[ServiceContract]
public interface IMathService
{
[OperationContract]
String HelloWorld();
}
}
Service configuration (Web.config):
<services>
<service name="Test.MathService" behaviorConfiguration="ServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:80/WCFtest/service.svc" />
</baseAddresses>
</host>
<endpoint address="/soap" binding="basicHttpBinding" contract="Test.IMathService" />
<endpoint address="/rest" binding="webHttpBinding" contract="Test.IMathService" behaviorConfiguration="WEB" />
</service>
</services>
[...]
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="http://localhost:81" />
<add name="Access-Control-Allow-Headers" value="SOAPAction, Content-type, Accept, Origin" />
<add name="Access-Control-Allow-Methods" value="POST, GET, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
JQuery code:
$('#btn_helloworld').click(function () {
var soapRequest = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
'<soapenv:Header/>' +
'<soapenv:Body>' +
'<tem:HelloWorld/>' +
'</soapenv:Body>' +
'</soapenv:Envelope>';
$.ajax({
type: 'POST',
url: 'http://localhost/WCFtest/service.svc/soap',
crossDomain: true,
contentType: 'text/xml',
dataType: 'xml',
data: soapRequest,
beforeSend: function (xhr) {
xhr.setRequestHeader('SOAPAction', 'http://tempuri.org/IMathService/HelloWorld');
},
success: function (data) {
$('#outputHelloWorld').val($(data).find('HelloWorldResponse').text());
},
error: function (jqxhr, textStatus, errorThrown) {
alert(jqxhr.responseXML + ' | ' + textStatus + ' | ' + errorThrown);
}
});
});
Service is working fine if called from the same origin.
Did I missed something?
After spending 2 days chasing ghosts (various versions of "interpretations" of CORS by the browsers) myself I think the conclusion is : the problem here is the OPTIONS request - FF and Opera are sending it, but I don't think IE does. Also I don't think OPTIONS is an allowed verb by default by IIS, IIS express, etc. I added Application_BeginRequest in my global.asax:
As a result I managed to get my request through with FF.