c# - 使 WCF 服务接受来自 jQuery.AJAX() 的 JSON 数据

标签 c# jquery json wcf endpoint

我已经搜索了几个小时并尝试了不同的方法来让它工作。我已经尝试了很多关于 stackoverflow 的文章,要么是我太笨了,无法让事情正常运行,要么是我有一些独特而奇怪的配置,使我无法体验快乐。

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

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

它是 super 基础的,只有一种方法,我想要它做的就是允许我使用 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>

最佳答案

改变这一行

data: {
    name: "Joe"
}

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

编辑:

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

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

关于c# - 使 WCF 服务接受来自 jQuery.AJAX() 的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15530412/

相关文章:

c# - 扩展 C# 合并运算符

jquery - 如何使用 jquery 动态添加/删除多个 div

javascript - Bootstrap 导航栏从静态到固定的饮食内容/抖动效果

javascript - 我得到一个 JSON/Javascript 对象,因为我可以在控制台中看到它,但如何让它序列化并使我的数据绑定(bind)到我的 Ui 元素?

javascript - 组合对象

c# - 查看当 c#/ASP.NET 线程终止时会发生什么以及如何解决问题

c# - 检查是否安装了 MS Access 2010

c# - 如何在 ASP.NET Core 中使用支持依赖注入(inject)的自定义模型联编程序?

javascript - Highcharts 仪表使工具提示可见并居中

android - 如何在 Kotlin 中将 ArrayList 转换为 JSONArray()