创建一个WCF服务从jQuery.AJAX()接受JSON数据

时间:2013-03-20 17:18:49

标签: c# jquery json wcf endpoint

我一直在寻找几个小时并尝试不同的方法来实现这一目标。我已经尝试了很多关于stackoverflow的文章,要么我太愚蠢了,不能让事情发生,或者我有一些独特而奇怪的配置让我无法体验到快乐。

我创建本教程概述的WCF服务:

http://www.codeproject.com/Articles/97204/Implementing-a-Basic-Hello-World-WCF-Service

它是超级基础并且有一个方法,我想要它做的就是允许我使用json使用jQuery.AJAX()。

我把它托管在IIS中,它可以工作。我可以毫无问题地访问WSDL。

我尝试使用以下代码使用它:

$.ajax({
    dataType: 'json',
    type: 'POST',
    contentType: "application/json",
    url: "//localhost:546/HelloWorldService.svc/GetMessage",
    data: {
        name: "Joe"
    }
    }).done(function(msg){
        console.log(msg);
        $("#result").append(msg);
});

我总是得到错误。根据我的尝试,我得到500个错误,402错误,错误内容错误......所有错误。

我尝试过以下文章中的解决方案。它们包括让我更改web.config端点(我知道我必须更改它们,但我迄今为止没有尝试过添加JSON端点的工作)来添加诸如

之类的内容
[WebInvoke(Method = "POST", UriTemplate = "json/PostSalesOrderData", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

到界面。

以下是我看过的一些文章,并试图粉碎我的解决方案,让它工作没有太大成功。

Javascript JSON and WCF webservice on Phonegap Android

HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'

WCF Services with JSON, JSONP and SOAP End Points

Two endpoint (soap ,json) and one service method

WCF REST Service not accepting JSON in .Net 4

我也仔细阅读了本教程并尝试使用他必须说的来使我的解决方案正常工作。仍然没有!

http://www.primordialcode.com/blog/post/passing-json-serialized-objects-wcf-service-jquery

这就是我的界面外观

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "GetMessage", Method = "POST",
        BodyStyle = WebMessageBodyStyle.WrappedRequest,
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json)]
    String GetMessage(String name);
}

任何人都可以帮助我体验快乐吗?

提前感谢您查看我的问题。如果您需要更多信息或我没有提供足够的信息,请告诉我,以便我帮助您!

我一定错过了一些愚蠢的东西......我知道这不是很难。

编辑:

使用Web.Config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="WebHTTPEndpointBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding name="MyWebServiceBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"/>
      </webHttpBinding>
    </bindings>
    <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>
  </system.serviceModel>
</configuration>

2 个答案:

答案 0 :(得分:5)

更改此行

data: {
    name: "Joe"
}

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

编辑:

对您的服务执行此操作。在配置中添加WebHttp绑定。

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

希望你知道在哪里添加这个。如果不让我知道,我会尝试提供一些意见。

修改

跟进我的评论,

<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>

答案 1 :(得分:1)

我来晚了,但是你仍然需要解决一些问题才能让它发挥作用。您需要更改端点的绑定以支持HTTP操作。

<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>

maxBufferSizemaxReceivedMessageSize是可选的。

编辑:哎呀,忘了添加behaviorConfiguration