jQuery調用WCF
在項目中用過一些WCF的技術
這篇文章是對以前用過的一點東西的一個梳理
一,webconfig的配置
除了一般的配置外,與WCF相關的配置如下
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AllenBehavior">
<enableWebScript />
behavior>
endpointBehaviors>
behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="jqueryWCF.WCFservice">
<endpoint address="" behaviorConfiguration="AllenBehavior" binding="webHttpBinding" contract="jqueryWCF.WCFservice" />
service>
services>
system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="AllenBehavior">
<enableWebScript />
behavior>
endpointBehaviors>
behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="jqueryWCF.WCFservice">
<endpoint address="" behaviorConfiguration="AllenBehavior" binding="webHttpBinding" contract="jqueryWCF.WCFservice" />
service>
services>
system.serviceModel>
其中節點中的name屬性,是實現了服務契約的類型名,類型名必須是完整的,要包括名稱空間
節點的address屬性為空,說明使用基地址.
behaviorConfiguration屬性與behavior節點的name屬性相匹配
binding屬性說明WCF服務使用什么協議,這里是HTTP協議
contract屬性是描述契約的接口名稱,也必須是完整的.如果沒有接口直接寫實現契約的類型名也可以(我這里就是這樣).
節點的信息是描述WCF服務端的一些特性,行為的
name屬性與前面說的behaviorConfiguration屬性一致
節點使我們的WCF支持ajax
與后端的AspNetCompatibilityRequirements配合使用
節點使我們的WCF支持ajax
與后端的AspNetCompatibilityRequirements配合使用
二:頁面中的js代碼
這段JS是寫在JQUERY框架下面的
這段JS是寫在JQUERY框架下面的
function callServer(){
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());
$.ajax({
type: 'post',
url: '/WCFservice.svc/InsertRow',
contentType: 'text/json',
data: '{"id":'+id+',"title":"'+title+'","content":"'+content+'"}',
success: function(msg) {
var a = eval('('+msg+')');
if(String(a.d).length>0){alert(a.d);}
else{alert("服務器超時");}
}
});
}
var id = Number($("#id").val());
var title = String($("#title").val());
var content = String($("#content").val());
$.ajax({
type: 'post',
url: '/WCFservice.svc/InsertRow',
contentType: 'text/json',
data: '{"id":'+id+',"title":"'+title+'","content":"'+content+'"}',
success: function(msg) {
var a = eval('('+msg+')');
if(String(a.d).length>0){alert(a.d);}
else{alert("服務器超時");}
}
});
}
其中
$.ajax(.....)是框架提供的一個調用ajax的方法,兼容目前大多數瀏覽器
$.ajax(.....)是框架提供的一個調用ajax的方法,兼容目前大多數瀏覽器
url: '/WCFservice.svc/InsertRow'
這里是WCF的地址+方法名
contentType: 'text/json',
這是以JSON的方式POST數據,當然也可以用XML的方式(要配合WCF后端的定義)
data: '{"id":'+id+',"title":"'+title+'","content":"'+content+'"}',
數據必須按照InsertRow方法的簽名傳遞(這里稍有不慎就出錯了,而且js的調試比較難搞)
success: function(msg) {}
成功后的回調函數,msg參數是一個object類型的,要eval()一下才能得到里面的數據
三:后端WCF代碼
系統要引用System.ServiceModel.Web的DLL默認是不引用的
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace jqueryWCF
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFservice
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string InsertRow(int id,string title,string content)
{
return string.Format("您輸入的標題是:{0}\n\n您輸入的內容是:{1}\n\n此文章的id是:{2}",title,content,id.ToString());
}
}
}
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
namespace jqueryWCF
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WCFservice
{
[OperationContract]
[WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public string InsertRow(int id,string title,string content)
{
return string.Format("您輸入的標題是:{0}\n\n您輸入的內容是:{1}\n\n此文章的id是:{2}",title,content,id.ToString());
}
}
}
系統要引用System.ServiceModel.Web的DLL默認是不引用的
ServiceContract屬性把此類型公開在WCF服務中
AspNetCompatibilityRequirements屬性確保端點使用了WEBHTTP綁定模型
與webconfig中的配合使用
OperationContract屬性把方法公開在WCF服務中
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json
說明傳遞近來的數據都是JSON形式的,只有兩種形式,一個是JSON,一個是XML.
(我覺得JSON更"對象"一點,XML更"數據"一點)
與webconfig中的配合使用
OperationContract屬性把方法公開在WCF服務中
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json
說明傳遞近來的數據都是JSON形式的,只有兩種形式,一個是JSON,一個是XML.
(我覺得JSON更"對象"一點,XML更"數據"一點)
BodyStyle = WebMessageBodyStyle.WrappedRequest
是把參數包裝一下
這樣可以傳遞多個參數進來,
這樣可以傳遞多個參數進來,
全站熱搜