[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; }
<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没有被发送,它正在被发送并且格式正确,我可以从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>
提前致谢
解决方法
data: "{d:{logonRequest:" + JSON.stringify(logonRequest) + "}}"
要么是这样,要么删除enableWebScript行为,但是,如果这样做,则必须应用webHttp行为.现在,不幸的是,默认编码是XML,并且行为不提供任何开关来控制整个服务的编码(MSFT的错误设计).所以,要么你必须通过设置Request / ResponseFormat属性来嫁给每个方法的特定编码,或者,我过去如何处理它,是因为我创建了一个“EnhancedWebHttpElement”,它应用了相同的WebHttpBehavior,但是给它配置级别控制它的各种属性.使用此配置的好处是,您现在可以使用ASP.NET AJAX编码通过不同的端点公开相同的WCF服务,另一端使用普通的JSON,甚至可以使用另一个端口的POX.
这是代码:
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>
更新:
上面的答案严格适用于.NET 3.x.如果您使用的是.NET 4.x,那么现在可以使用WebHttpElement
上公开的the DefaultOutgoingResponseFormat
property来控制它.更好的是,您可以公开单个端点并设置the new AutomaticFormatSelectionEnabled
property,它将根据原始请求的格式以正确的格式进行响应.