Wcf self hosting jquery

I try to call the wcf self-service method from jquery, but I always get the message: "method not allowed." I found the answers to this question on this forum, but nothing helped me .... ps. It works great when I add a link to a console application and consume it.

this window creates a self-service application

Form loading:

ServiceHost host = new ServiceHost(typeof(MyServices), new Uri[] { });
host.Open();

App.Config

<system.serviceModel>
        <services>
            <service behaviorConfiguration="ServiceConfig" name="MyServices">
                <endpoint address="srv" binding="basicHttpBinding" contract="Operations" />
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
                <host>
                    <baseAddresses>
                        <add baseAddress="http://localhost:8766" />
                    </baseAddresses>
                </host>
            </service>
        </services>
        <behaviors>
            <serviceBehaviors>
                <behavior name="ServiceConfig">
                    <serviceMetadata httpGetEnabled="true" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    </system.serviceModel>

Operational contract:

[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
string Test();

Services:

public string Test()
{
return "Good!";
}

JQuery

$.ajax({
    url: "http://localhost:8766/Test",
    contentType: "application/json",
    processData: false,
    //data: '{}', // tried passing data
    type: "GET", // tried POST and pass nothing(deleting this param), but nothing...
    success: function (msg) {
        console.log(msg);
    }
});
+3
source share
1 answer

Well, your current problem is caused by the fact that WebInvokewhen you do not specify a parameter Method, the default will be "POST". Your jQuery code issues a "GET" request, so a "method not allowed" error is expected.

GET , . Windows http://localhost:8766, , - `http://localhost:8766. :

"" , ( ) TCP HTML, script. , .

JSON, GET, JSONP. , . jQuery :

$.ajax({
    url: "http://localhost:8766/Test",
    dataType: "jsonp",
    processData: false,
    type: "GET",
    success: function (msg) {
        console.log(msg);
    }
});

WebInvoke, WebHttpBinding BasicHttpBinding. WebInvoke , WebHttpBinding. WebHttpBinding, , JSONP. , :

<system.serviceModel>
    <bindings>
        <webHttpBinding>
            <binding name="webHttpBindingWithJsonP" crossDomainScriptAccessEnabled="true" />
        </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
          <behavior name="webHttpBehavior">
            <webHttp />
          </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
        <service behaviorConfiguration="ServiceConfig" name="MyServices">
            <endpoint address="srv" binding="webHttpBinding" contract="Operations" bindingConfiguration="webHttpBindingWithJsonP" behaviorConfiguration="webHttpBehavior" />
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8766" />
                </baseAddresses>
            </host>
        </service>
    </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceConfig">
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
</system.serviceModel>

post, .

, !

+2

Source: https://habr.com/ru/post/1764145/


All Articles