Make a WCF Service Accept JSON Data from jQuery.AJAX()

后端 未结 2 1700
伪装坚强ぢ
伪装坚强ぢ 2021-01-13 12:49

I have been searching around for hours and trying different things to get this to work. I have tried so many articles on stackoverflow and either I am too stupid to get thin

相关标签:
2条回答
  • 2021-01-13 13:30

    I'm coming in late, but you still have some issues you'll need to resolve to get it to work. You need to change the binding for your endpoint to support the HTTP operations.

    <bindings>
        <webHttpBinding>
            <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
        </webHttpBinding>
    </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="WebHTTPEndpointBehavior">
              <webHttp helpEnabled="true"/>
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <services>
      <service name="MyWCFServices.HelloWorldService"
             behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="webHttpBinding" bindingConfiguration="MyWebServiceBinding" behaviorConfiguration="WebHTTPEndpointBehavior"
             contract="MyWCFServices.IHelloWorldService"/>
        <endpoint contract="IMetadataExchange"
           binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
    

    The maxBufferSize and maxReceivedMessageSize is optional.

    EDIT: Oops, forgot to add your behaviorConfiguration in.

    0 讨论(0)
  • 2021-01-13 13:35

    change this line

    data: {
        name: "Joe"
    }
    

    to

    data: JSON.stringify({name: 'Joe'});
    

    EDIT:

    Do this to your service. Add WebHttp binding in the config.

     <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    

    Hope you know where to add this. If not let me know and I will try to provide some inputs.

    EDIT:

    Following up on my comment,

    <behavior name="myBehaviorName">
          <webHttp />
    </behavior>
    
    <service name="MyWCFServices.HelloWorldService"
             behaviorConfiguration="MyServiceTypeBehaviors">
        <endpoint address="" binding="webHttpBinding"
             contract="MyWCFServices.IHelloWorldService" behaviorConfiguration="myBehaviorName"/>
        <endpoint contract="IMetadataExchange"
           binding="mexHttpBinding" address="mex"/>
      </service>
    
    0 讨论(0)
提交回复
热议问题