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?