我有一个通过我的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内部服务器错误消息并且异常开始
{"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>
提前致谢
答案 0 :(得分:13)
我猜你已经应用了WebScriptEnablingBehavior(.config中的enableWebScript元素)?这会导致所有请求都包装在一个JSON对象中,该对象具有一个名为“d”的字段,该字段随后包含一个带有命名参数的对象。因此,您需要将jQuery数据更改为:
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,那么现在可以使用the DefaultOutgoingResponseFormat
property上公开的WebHttpElement
来控制它。更好的是,您可以公开单个端点并设置the new AutomaticFormatSelectionEnabled
property,它将根据原始请求的格式以正确的格式进行响应。