我有一个通过我的ASPX网站公开的WCF服务库,如下所示
[System.ServiceModel.OperationContract] [System.ServiceModel.Web.WebInvoke( Method= "POST", RequestFormat=System.ServiceModel.Web. WebMessageFormat .Json, ResponseFormat=System.ServiceModel.Web.WebMessageFormat .Json)] LogonResponse Logon(LogonRequest logonRequest); [System.Runtime.Serialization.DataContract] [ Serializable()] public class LogonRequest { [System.Runtime.Serialization.DataMember] public string EMailAddress; [System.Runtime.Serialization.DataMember] public string Password; }
在我的测试页中,我可以通过MS Ajax进行呼叫:-
<asp:ScriptManager ID ="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/testService.svc" /> </Services> </asp:ScriptManager> . . . function clsLogonRequest(eMailAddress, password) { this .EMailAddress = eMailAddress; this .Password = password; } function login(eMailAddress, password) { var LogonRequest = new clsLogonRequest(eMailAddress, password); name.API.IAPI.Logon(LogonRequest, onSuccess, onFailure); } function onSuccess(result) { $( "#txtSessionId").val(result.SessionId); $( "#txtUserName").val(result.Name); $( "#txtUserId").val(result.UserId); }
效果很好,或通过jQuery $ .ajax调用:-
$(document).ready(function() { $( "#Button1").click(function() { var LogonRequest = new clsLogonRequest( '*************' , '***********'); $.ajax({ type: "POST", url: "testService.svc/Logon", data: "{'logonRequest':" + JSON.stringify(LogonRequest) + "}" , contentType: "application/json; charset=utf-8", dataType: "json", success: function(msg) { alert(msg.d); } }); }); });
哪个不行-在FireBug下,我可以看到500 Internal Server Error消息,并且异常开始
{"ExceptionDetail":{"HelpLink":null,"InnerException":null,"Message":"The token '\"' was expected but found '''.","StackTrace":" at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)\u000d\u000a at System.Xml.XmlExceptionHelper .ThrowTokenExpected(XmlDictionaryReader reader, String expected, Char found)\u000d\u000a at System.Runtime...
当我明确告诉它不这样做时,为什么看起来jQuery调用正在传递XML(以及如何停止它,以便尽可能自然地处理来自所有来源的调用)?
提前致谢。
编辑:
感谢您的建议,我查看了您的发言,并认为我对该问题的解释不够清楚。
问题不在于没有发送JSON,而是正在发送JSON,并且格式正确,我可以从firebug中看到。问题在于WCF服务库似乎期望XML,并且在接收JSON时掉线。
为了确保我没有忽略建议的解决方案,这里是web.Config摘录-我尝试从行为中删除并从services / service / endpoint标记中删除behaviorConfiguration属性,但这只会使整个操作失败而不离开页面。
<system.serviceModel> <behaviors> <serviceBehaviors> <behavior name ="MyServiceTypeBehaviors "> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceMetadata httpGetEnabled="true" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name ="AspNetAjaxBehavior"> <enableWebScript/> </behavior> </endpointBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled= "true " /> <services> <service name ="test.API.API" behaviorConfiguration = " MyServiceTypeBehaviors"> <endpoint behaviorConfiguration="AspNetAjaxBehavior" binding = " webHttpBinding" contract="test.API.IAPI" /> <endpoint contract ="IMetadataExchange" binding= " mexHttpBinding" address="mex" /> </service> </services> </system.serviceModel>
提前致谢
我猜您已经应用了WebScriptEnablingBehavior(.config中的enableWebScript元素)?这会导致所有请求都包装在一个名为“ d”的字段中的JSON对象中,该字段随后包含带有您命名参数的对象。因此,您需要将jQuery数据更改为:
data: "{d:{logonRequest:" + JSON.stringify(LogonRequest) + "}}"
要么删除enableWebScript行为,但如果这样做,则必须改为应用webHttp行为。现在,不幸的是,默认编码为XML,并且该行为没有提供任何开关来控制整个服务的编码(MSFT的不良设计)。因此,您必须通过设置Request / ResponseFormat属性来对每种方法使用特定的编码,或者我过去的处理方式是创建了一个应用了相同WebHttpBehavior的“ EnhancedWebHttpElement”,但可以对其各种属性进行配置级别控制。使用此配置的好处是,您现在可以通过在一个端点上使用ASP.NET AJAX编码,在另一个端点上使用纯JSON,甚至在另一个端点上使用POX,通过不同的端点公开相同的WCF服务。
这是代码:
public sealed class EnhancedWebHttpElement : BehaviorExtensionElement { #region Type specific properties [ConfigurationProperty("defaultBodyStyle", DefaultValue=WebMessageBodyStyle.Bare)] public WebMessageBodyStyle DefaultBodyStyle { get { return (WebMessageBodyStyle)this["defaultBodyStyle"]; } set { this["defaultBodyStyle"] = value; } } [ConfigurationProperty("defaultOutgoingRequestFormat", DefaultValue=WebMessageFormat.Xml)] public WebMessageFormat DefaultOutgoingRequestFormat { get { return (WebMessageFormat)this["defaultOutgoingRequestFormat"]; } set { this["defaultOutgoingRequestFormat"] = value; } } [ConfigurationProperty("defaultOutgoingResponseFormat", DefaultValue=WebMessageFormat.Xml)] public WebMessageFormat DefaultOutgoingResponseFormat { get { return (WebMessageFormat)this["defaultOutgoingResponseFormat"]; } set { this["defaultOutgoingResponseFormat"] = value; } } #endregion #region Base class overrides protected override object CreateBehavior() { WebHttpBehavior result = new WebHttpBehavior(); result.DefaultBodyStyle = this.DefaultBodyStyle; result.DefaultOutgoingRequestFormat = this.DefaultOutgoingRequestFormat; result.DefaultOutgoingResponseFormat = this.DefaultOutgoingResponseFormat; return result; } public override Type BehaviorType { get { return typeof(WebHttpBehavior); } } #endregion }
然后,您必须注册它:
<system.serviceModel> <extensions> <behaviorExtensions> <add name="enhancedWebHttp" type="MyNamespace.EnhancedWebHttpElement, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> </behaviorExtensions> </extensions>
然后像这样使用它:
<behavior name="MyBehavior"> <enhancedWebHttp defaultOutgoingRequestFormat="JSON" defaultOutgoingResponseFormat="JSON" /> </behavior>
更新:
上面的答案完全适用于.NET3.x。如果使用的是.NET 4.x,则现在可以使用公开的DefaultOutgoingResponseFormat属性WebHttpElement来控制它。更好的是,您可以公开一个端点并设置新AutomaticFormatSelectionEnabled属性,该属性将根据原始请求的格式以正确的格式进行响应。
DefaultOutgoingResponseFormat
WebHttpElement
AutomaticFormatSelectionEnabled