问题从jQuery调用WCF服务库

前端之家收集整理的这篇文章主要介绍了问题从jQuery调用WCF服务库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个通过我的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没有被发送,它正在被发送并且格式正确,我可以从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元素)?这会导致所有请求都包装在一个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,那么现在可以使用WebHttpElement上公开的the DefaultOutgoingResponseFormat property来控制它.更好的是,您可以公开单个端点并设置the new AutomaticFormatSelectionEnabled property,它将根据原始请求的格式以正确的格式进行响应.

原文链接:https://www.f2er.com/jquery/177754.html

猜你在找的jQuery相关文章