Call WCF service from JQuery : cross-origin OPTION

2020-03-05 20:15发布

问题:

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.

  1. Using Chrome, I get expected results, but looking at the network traffic, it shows that the OPTIONS request returns error 400 Bad Request, However the next POST request succeeds :

  2. Using Firefox, error is raised (JavaScript alert shows null | error |). It seems the POST request is not sent because the OPTIONS request failed:

  3. 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?

回答1:

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:

void Application_BeginRequest(Object sender, EventArgs e)
{
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

    if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "*");

        HttpContext.Current.Response.End();
    }
}

As a result I managed to get my request through with FF.