签订合同:
namespace ACME.FooServices { [ServiceContract] public interface IFooService { [OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] FooMessageType Foo(string name); } [DataContract] public class FooMessageType { string _name; string _date; [DataMember] public string Name { get { return _name; } set { _name = value; } } [DataMember] public string Date { get { return _date; } set { _date = value; } } } }
并实现:
using System; using System.ServiceModel.Activation; namespace ACME.FooServices { [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] public class FooService : IFooService { public FooMessageType Foo(string name) { string l_name = (String.IsNullOrWhiteSpace(name)) ? "Anonymous" : name; return new FooMessageType {Name = l_name, Date = DateTime.Now.ToString("MM-dd-yyyy h:mm:ss tt")}; } } }
在web.config中配置为:
<system.serviceModel> <services> <service name="ACME.FooServices.FooService"> <endpoint address="" behaviorConfiguration="ACME.FooBehaviour" binding="webHttpBinding" contract="ACME.FooServices.IFooService" /> </service> </services> <behaviors> <endpointBehaviors> <behavior name="ACME.FooBehaviour"> <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /> </system.serviceModel>
我正在尝试通过jQuery从页面调用Foo:
<script type="text/javascript" language="javascript"> $(document).ready(function () { $("#msgButton").click(function () { var params = {}; params.name = $("#nameTextbox").val(); $.ajax({ type: 'POST', url: "http://acme.com/wcfsvc/FooService.svc/Foo", data: JSON.stringify(params), contentType: 'application/json; charset=utf-8', success: function (response, status, xhr) { alert('success: ' + response); }, error: function (xhr, status, error) { alert("Error\n-----\n" + xhr.status + '\n' + xhr.responseText); }, complete: function (jqXHR, status) { alert('Status: ' + status + '\njqXHR: ' + JSON.stringify(jqXHR)); } }); }); }); </script>
但是我收到一条消息 400-错误的请求 错误,消息为 “服务器在处理请求时遇到错误。异常消息是’在反序列化类型为System.String的对象时发生错误。从名称空间’结束的元素’root’ ‘预期。从名称空间找到了元素’name’ 。
我想念什么吗?
您params是对象,它形成{ "name" : "someValue" }JSON字符串。如果您说的是邮件正文样式,Bare我认为您的服务期望这样的内容:
params
{ "name" : "someValue" }
Bare
[DataContract] public class SomeDTO { [DataMember(Name = "name")] public string Name { get; set; } }
因此,您的操作应定义为:
[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] FooMessageType Foo(SomeDTO data);
如果您希望当前代码正常工作,则应将其更改为:
[OperationContract] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)] FooMessageType Foo(SomeDTO data);