【发布时间】: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
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