Ajax POST to WCF Rest CORS-compliant WebService th

2019-08-01 06:35发布

问题:

I'm doing some test over WCF REST WebServices and i'm stuck with the POST call. I've created a webservice that exposes some test data about the good ol' Northwind DB and since i wish to consume it locally from a test HTML page and since i'd like to test CORS capabilities, i made it CORS compliant by following these instruction http://enable-cors.org/server_wcf.html.

Unfortunately problems comes out when i make POST calls. Unlike GET calls (works very well), POST call throws this error:

What the hell is it? it seems that "Access-Control-Allow-Origin" header is not correctly managed client-side, beacuse in my EnableCrossOriginResourceSharingBehavior WCF class, the method "ApplyDispatchBehavior" (it filter "Access-Control-Allow-Origin" headers of the arrival requests) is hit when i make a POST call, but then Ajax call fails.

This is my jQuery Ajax post command:

       //Create new object
        var item = {
            "CustomerId": "0",
            "CompanyName": "prova"
        };

        //Push object
        $.ajax({
            type: "POST",
            url: 'http://localhost:3434/NorthwindService.svc/Customer/Create',
            crossDomain: true,
            headers: {'Access-Control-Allow-Origin' : '*'},
            data: JSON.stringify(item),
            success: function (data) {
                alert('ok!');
            },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json'
        });

This is my WCF service Visual Studio 2013 project. To test it, you only have to set "NorthwindConnectionString" in web.config to an existing one. The webservice method that i've problem with, is the POST to the "http://localhost:3434/NorthwindService.svc/Customer/Create" method, all the others works fine. This is a preview of my method contract:

 [OperationContract]
 [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "Customer/Create", BodyStyle=WebMessageBodyStyle.WrappedRequest)]
 void NewCustomer(CustomerDTO customer);

Thanks in advance.

回答1:

Your HTTP Request Method is defined OPTIONS instead of POST.

That is why you get HTTP Response 405 Method not Allowed (no handler for OPTIONS request)

Change the type parameter in jQuery ajax constructor to "POST" and the request is routed to correct handler.



回答2:

I don't know what's going on, but thanks to supertopi and his link, i did the right steps to make it works. Unfortunately implementing all things discussed in here How to handle Ajax JQUERY POST request with WCF self-host did't works. I continued to get "405 Method not allowed" even by creating a new project. The only thing that works in my case is the following:

1) Implement CustomHeaderMessageInspector and EnableCrossOriginResourceSharingBehavior classes and edit web.config as exposed in http://enable-cors.org/server_wcf.html.

2) Create in the service contract the following method:

[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
 void GetOptions();

3) Implementing it empty.

public void GetOptions()
{

}

It sounds crazy, but it actually works. If i remove GetOptions() operation contract, i continue to get 405 error on my client. If i implement it like indicated by supertopi's link (obviously after remove all stuff created in the step 1), it doesn't work either.

Hope it helps.