Jquery 通过 Json 连接 WCF 例子
Jquery 代码
Code
$(function(){
$("#loginValidTd").click(function(){
$.ajax({
type: "POST",
contentType:"application/json",
url: "WCFServices/UserRightValid.svc/LoginValid",
data:"{\"userName\":\""+$("#UserName").val()+"\",\"password\":\""+$("#Password").val()+"\"}",//这一句非常重要,如果你要调用的方法是没有参数的
processData:false,
dataType:"json",
success: function(data){
if(data)
{
alert("成功");
}
else
{
alert("用户名或者密码不正确!");
}
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert( "Error Occured!" );
}
});
});
});
$(function(){
$("#loginValidTd").click(function(){
$.ajax({
type: "POST",
contentType:"application/json",
url: "WCFServices/UserRightValid.svc/LoginValid",
data:"{\"userName\":\""+$("#UserName").val()+"\",\"password\":\""+$("#Password").val()+"\"}",//这一句非常重要,如果你要调用的方法是没有参数的
processData:false,
dataType:"json",
success: function(data){
if(data)
{
alert("成功");
}
else
{
alert("用户名或者密码不正确!");
}
},
error:function(XMLHttpRequest, textStatus, errorThrown){
alert( "Error Occured!" );
}
});
});
});
WCF 代码
Code
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Security;
namespace ClientInfo.Web.WCFServices
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserRightValid
{
// 添加 [WebGet] 属性以使用 HTTP GET
[OperationContract]
public void DoWork()
{
// 在此处添加操作实现
return;
}
// 在此处添加更多操作并使用 [OperationContract] 标记它们
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public bool LoginValid(string userName,string password)
{
bool result = false;
result=Membership.ValidateUser(userName, password);
return result;
}
}
}
using System;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Security;
namespace ClientInfo.Web.WCFServices
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserRightValid
{
// 添加 [WebGet] 属性以使用 HTTP GET
[OperationContract]
public void DoWork()
{
// 在此处添加操作实现
return;
}
// 在此处添加更多操作并使用 [OperationContract] 标记它们
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public bool LoginValid(string userName,string password)
{
bool result = false;
result=Membership.ValidateUser(userName, password);
return result;
}
}
}
Webconfig 注意注释的地方
Code
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ClientInfo.Web.WCFServices.UserRightValidAspNetAjaxBehavior">
<!--<enableWebScript />--><!--用到WCF,注释此项-->
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="ClientInfo.Web.WCFServices.UserRightValid">
<endpoint address="" behaviorConfiguration="ClientInfo.Web.WCFServices.UserRightValidAspNetAjaxBehavior"
binding="webHttpBinding" contract="ClientInfo.Web.WCFServices.UserRightValid" />
</service>
</services>
</system.serviceModel>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="ClientInfo.Web.WCFServices.UserRightValidAspNetAjaxBehavior">
<!--<enableWebScript />--><!--用到WCF,注释此项-->
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="ClientInfo.Web.WCFServices.UserRightValid">
<endpoint address="" behaviorConfiguration="ClientInfo.Web.WCFServices.UserRightValidAspNetAjaxBehavior"
binding="webHttpBinding" contract="ClientInfo.Web.WCFServices.UserRightValid" />
</service>
</services>
</system.serviceModel>