【问题标题】:Make a WCF Service Accept JSON Data from jQuery.AJAX()使 WCF 服务接受来自 jQuery.AJAX() 的 JSON 数据
【发布时间】:2013-03-09 22:51:49
【问题描述】:

我已经搜索了好几个小时并尝试了不同的方法来让它发挥作用。我已经尝试了很多关于 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>

【问题讨论】:

    标签: c# jquery json wcf endpoint


    【解决方案1】:

    改变这一行

    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>
    

    【讨论】:

    • 感谢您的回复。我仍然收到 500 错误...我认为问题出在 service/web.config 不一定是 .ajax 调用。
    • 错误描述是什么?并且您的 url 状态为“url:”//localhost:546/HelloWorldService.svc/HelloWorldService”但是您正在拨打的电话是 GetMessage?这里有什么问题吗?
    • 哦,我的错!为您的服务做到这一点。
    • 编辑了我的答案。抱歉,评论太多了。
    • 将 wsBinding 更改为 webHttpBinding,为您定义 WebHttp 的行为命名,并在您的终点添加一个 behaviorConfiguration 作为该名称
    【解决方案2】:

    我来晚了,但您仍有一些问题需要解决才能使其正常工作。您需要更改端点的绑定以支持 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

    【讨论】:

    • 将 helpEnabled="true" 添加到 webHttp 行为不会有什么坏处。它将允许您查看可用的操作。当您导航到服务绑定点时。
    • 好点。我在我的服务上使用它,我只是不想添加任何额外的东西,所以 OP 可以看到最低要求是什么。我会更新我的答案。
    • 这是一个巨大的帮助。该服务至少现在正在运行。该服务告诉我“方法不允许”,但我认为这超出了我原来帖子的范围。
    猜你喜欢
    • 2011-10-18
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 1970-01-01
    相关资源
    最近更新 更多