惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Help Net Security
Help Net Security
有赞技术团队
有赞技术团队
博客园 - 【当耐特】
O
OpenAI News
美团技术团队
月光博客
月光博客
Apple Machine Learning Research
Apple Machine Learning Research
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
Cyberwarzone
Cyberwarzone
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google Online Security Blog
Google Online Security Blog
T
Tenable Blog
S
Security Affairs
博客园_首页
S
Schneier on Security
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
Spread Privacy
Spread Privacy
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
TaoSecurity Blog
TaoSecurity Blog
博客园 - 聂微东
Vercel News
Vercel News
M
MIT News - Artificial intelligence
T
Troy Hunt's Blog
B
Blog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
Attack and Defense Labs
Attack and Defense Labs
L
LINUX DO - 最新话题
D
DataBreaches.Net
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Stack Overflow Blog
Stack Overflow Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - Franky
W
WeLiveSecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Fortinet All Blogs
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Check Point Blog
H
Hacker News: Front Page

博客园 - codingsilence

更新部分字段 NHibernate 都是分号惹的祸(ORA-00911: invalid character) 降低Winform 内存占用 installshiled 创建iis应用程序池和站点 FARPOINT 常见用法 比较Double型数据时的注意事项 WinForm中获取鼠标当前位置 查看SQL Server日志 WinForm winform动态生成菜单 WinForm 从XML中动态加载菜单的示例 “轻松加愉快”地实现并使用IComparer接口 WinForm 选择器 C#程序实现动态调用DLL的研究 WCF 的 WebGet 方式 开源选型关注点 在64位Windows的IIS上开启32位程序支持(转) Oracle grant用户授权 ODP.NET开发和部署的相关问题 Client使用c#和odp.net连接server oracle
WCF WebGet WebInvoke WCF Jquery 调用
codingsilence · 2011-07-18 · via 博客园 - codingsilence

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)]