从jQuery作为JSON使用WCF

前端之家收集整理的这篇文章主要介绍了从jQuery作为JSON使用WCF前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
与合同:
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,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类型的对象的错误.从命名空间’ ‘expected.从命名空间找到元素’name’.

我错过了什么吗?

解决方法

你的参数是对象,它形成{“name”:“someValue”} JSON字符串.如果你说消息体的样式是裸的,我想你的服务期望像这样:
[DataContract]
public class SomeDTO
{
    [DataMember(Name = "name")]
    public string Name { get; set; }
}

并且因为您的操作应定义为:

[OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Bare)]        
FooMessageType Foo(SomeDTO data);

如果您希望当前的代码可以工作,您应该将其更改为:

[OperationContract]
[WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.WrappedRequest)]        
FooMessageType Foo(SomeDTO data);
原文链接:https://www.f2er.com/jquery/176038.html

猜你在找的jQuery相关文章