WCF WebGet WebInvoke WCF Jquery 调用

1.创建WCF服务

 直接在网站中添加 Ajax-enabled-WCF Services ,命名为AjaxWcfServices.svc

代码如下:

[ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class HelloWorld
    {
        // 添加 [WebGet] 属性以使用 HTTP GET
        [OperationContract]
        [WebGet(RequestFormat=WebMessageFormat.Json)]
        public void DoWork(Person p)
        {
            // 在此处添加操作实现
            return;
        }

        [OperationContract]
        [WebInvoke(RequestFormat=WebMessageFormat.Json,
		ResponseFormat=WebMessageFormat.Json,
		BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public string DoWorkPost(string id, string title, string content)
        {
            return string.Format("您输入的标题是:{0}/n/n您输入的内容是:{1}/n/n此文章的id是:{2}", title, content, id.ToString());
        }

        [OperationContract]
        [WebInvoke(RequestFormat=WebMessageFormat.Json,
		ResponseFormat=WebMessageFormat.Xml,
		BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public Person DoWorkXml(string personName)
        {
            Person p = new Person();
            p.Name = personName;
            p.Age = 25;
            p.Shoes = new List<string>() {"shoes1","shoes2" };
            return p;
        }
        // 在此处添加更多操作并使用 [OperationContract] 标记它们
    }


 

2、客户端代码

 function SendAjaxWithPost(id, title, content) {

            $.ajax({

                type: "post",

                contentType: "text/json",

                dataType:"json",

                url: "WCFServices/HelloWorld.svc/DoWorkPost",

                data: '{"id":' + id + ',"title":"' + title + '","content":"' + content + '"}',

                success: function(msg) {

                    alert(msg);

                    if (msg.d) {

                        alert(msg.d);
                    }

                },

                error: function(XMLHttpRequest, textStatus, errorThrow) {

                    alert("Error Occured!");
                }

            });

        }

         function WcfAjaxXml(personName) {

            $.ajax({

                type: "post",

                contentType: "text/json",

                dataType: "text/xml",

                url: "WCFServices/HelloWorld.svc/DoWorkXml",

                data: '{"personName":"' + personName + '"}',

                success: function(msg) {

                    alert(msg);

                    var result = "";

                    result += "Name:" + $(msg).find("Name").text() + ",";

                    result += "Age:" + $(msg).find("Age").text() + ",";

                    result += "Shoes:" + $(msg).find("Shoes").text();

                    alert(result);

                },

                error: function(XMLHttpRequest, textStatus, errorThrow) {

                    alert("Error Occured!");

                }

            });

        }


在用Jquery ajax调用WCF服务传递json对象时,在分别用Post,get数据方式时,设置json参数格式时需要采用不同的格式类型。

Get类型:参数传递格式:{ "name": name }

Post类型:参数传递格式:'{"name":"'+name+'"}' 如果用Get类型那样传参会在Wcf接受的时候会提示json格式错误

在用Post类型提交时,相应的WCF服务 [WebGet()]修改成相应的 [WebInvoke()],WCF默认传递格式为json,也可显示的添加为[WebGet(ResponseFormat=WebMessageFormat.Xml)]或者[WebInvoke(RequestFormat=WebMessageFormat.Xml)]

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值