jQuery调用WCF返回JSON对象
2011-01-13 16:58
423 查看
第一步:自定义数据类型(最终此类型以JSON的格式返回到客户端供解析)
JsonResult.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
[DataContract]
public class JsonResult
{
public JsonResult(string name, string address, string phone)
{
_name = name ;
_address = address;
_phone = phone;
}
private string _name;
[DataMember]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _address;
[DataMember]
public string Address
{
get { return _address; }
set { _address = value; }
}
private string _phone;
[DataMember]
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
}
第二步:构建wcf服务
Service.svc 文件代码:
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
IService.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
JsonResult GetJsonResult(string name, string address, string phone);
}
Service.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
public class Service : IService
{
public JsonResult GetJsonResult(string name, string address, string phone)
{
JsonResult result = new JsonResult(name, address, phone);
return result;
}
}
第三步:客户端调用wcf服务,并读取自定义数据类型(在客户端以JSON格式返回)
<script type="text/javascript" language="javascript">
function Call() {
$("#divMessagePanel").html("");
var formativeData = '{"name":"张三","address":"李四","phone":"王五"}';
$.ajax({
type: "post",
url: "../Service.svc/ajaxEndpoint/GetJsonResult",
contentType: "application/json;charset=utf-8",
data: formativeData,
success: function(data) {
alert(data);
var a = eval_r('(' + data + ')');
alert(a.GetJsonResultResult.Name);
$('#divMessagePanel').html("ok");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#divMessagePanel").html("error");
},
cache: false
});
}
</script>
web.config中wcf的配置部分:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="jsonWcfBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service">
<!--注意此处的endpoint配置,address和contract两个属性,在客户端Js调用时会用的上-->
<endpoint address="ajaxEndpoint" behaviorConfiguration="jsonWcfBehavior" binding="webHttpBinding" contract="IService">
</endpoint>
</service>
</services>
</system.serviceModel>
JsonResult.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.Serialization;
[DataContract]
public class JsonResult
{
public JsonResult(string name, string address, string phone)
{
_name = name ;
_address = address;
_phone = phone;
}
private string _name;
[DataMember]
public string Name
{
get { return _name; }
set { _name = value; }
}
private string _address;
[DataMember]
public string Address
{
get { return _address; }
set { _address = value; }
}
private string _phone;
[DataMember]
public string Phone
{
get { return _phone; }
set { _phone = value; }
}
}
第二步:构建wcf服务
Service.svc 文件代码:
<%@ ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
IService.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
[ServiceContract]
public interface IService
{
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
JsonResult GetJsonResult(string name, string address, string phone);
}
Service.cs 文件代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
public class Service : IService
{
public JsonResult GetJsonResult(string name, string address, string phone)
{
JsonResult result = new JsonResult(name, address, phone);
return result;
}
}
第三步:客户端调用wcf服务,并读取自定义数据类型(在客户端以JSON格式返回)
<script type="text/javascript" language="javascript">
function Call() {
$("#divMessagePanel").html("");
var formativeData = '{"name":"张三","address":"李四","phone":"王五"}';
$.ajax({
type: "post",
url: "../Service.svc/ajaxEndpoint/GetJsonResult",
contentType: "application/json;charset=utf-8",
data: formativeData,
success: function(data) {
alert(data);
var a = eval_r('(' + data + ')');
alert(a.GetJsonResultResult.Name);
$('#divMessagePanel').html("ok");
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#divMessagePanel").html("error");
},
cache: false
});
}
</script>
web.config中wcf的配置部分:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="jsonWcfBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="Service">
<!--注意此处的endpoint配置,address和contract两个属性,在客户端Js调用时会用的上-->
<endpoint address="ajaxEndpoint" behaviorConfiguration="jsonWcfBehavior" binding="webHttpBinding" contract="IService">
</endpoint>
</service>
</services>
</system.serviceModel>
相关文章推荐
- jQuery调用WCF服务传递JSON对象
- jQuery调用WCF服务传递JSON对象
- 艾伟:WCF以Json格式返回对象,客户端以JS调用显示
- WCF以Json格式返回对象,客户端以JS调用显示
- jquery 跨域调用wcf 返回json 碰到的一些问题
- [译]jQuery调用WCF服务传递JSON对象
- WCF服务——jQuery调用传递JSON对象、XML对象
- jQuery调用WCF服务传递JSON对象
- jQuery调用ajax返回json对象
- 使用jQuery匹配文档中所有的li元素,返回一个jQuery对象,然后通过数组下标的方式读取jQuery集合中第1个DOM元素,此时返回的是DOM对象,然后调用DOM属性innerHTML,读取该元素 包含的文本信息
- liferay中使用Jquery返回json对象处理
- jquery与后台进行数据交互--前台发单一数据对象,后台返回一个json字符串 jsp页面
- jQuery asp.net 用json格式返回自定义对象
- jquery调用基于.NET Framework 3.5的WebService返回JSON数据
- jquery中ajax如何返回值到上层函数的方法以及对于js处理json对象方法的记录
- jquery-autoComplete 处理返回的json对象问题
- jQuery调用WebService返回JSON数据及参数设置注意问题
- jQuery调用WebService返回JSON数据
- jquery调用基于.NET Framework 3.5的WebService返回JSON数据
- Node.js 使用jQuery取得Nodejs http服务端返回的JSON对象示例