WCF WebGet WebInvoke WCF Jquery 调用
2011-07-18 09:44
281 查看
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)]
直接在网站中添加 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)]
相关文章推荐
- WCF WebGet WebInvoke WCF Jquery 调用
- WCF WebGet WebInvoke WCF Jquery 调用
- jQuery调用RESTful WCF示例代码(GET方法/POST方法)
- jquery使用json格式 调用 restful wcf web service
- wcf指定要序列化多个请求正文参数,但没有任何包装元素。如果没有包装元素,至多可序列化一个正文参数。请删除多余的正文参数,或将 WebGetAttribute/WebInvokeAttribute 的
- jQuery调用RESTful WCF示例代码(GET方法/POST方法)
- jquery使用json格式 调用 restful wcf web service
- WCF 第十三章 可编程站点 使用WebGet和WebInvoke
- jQuery调用RESTful WCF示例(GET方法/POST方法)
- 化零为整WCF(18) - Web编程模型(WCF创建REST, AJAX调用WCF)
- 使用java反射中的getMethod,invoke方法调用对象方法的实例
- jQuery直接调用asp.net后台WebMethod方法
- 抛弃WebService 在.NET4中用jQuery调用WCF
- WebDriver中调用JQuery(通过读取本地JQuery来实现)
- HTTP-GET调用Web服务
- WF工作流与Web服务的相互调用 —— WF与WCF互相调用(利用ReceiveActivity把WF发布为WCF)
- Jquery调用WCF经验分享
- WCF 的 WebGet 方式
- jquery 跨域调用wcf 返回json 碰到的一些问题
- Java与WCF交互(二):WCF客户端调用Java web service