<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestWcf.aspx.cs" Inherits="WebApplication1.TestWcf" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" id="send" value="请求" />
<div id="resText">
</div>
</div>
</form>
</body>
</html>
<script src="Scripts/jquery-1.10.2.min.js"></script>
<script src="Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript" >
$(function () {
$('#send').click(function () {
//alert("111");
//获取参数
var PostParam = {
'UserInfo': JSON.stringify({
'Name': escape("花千骨"), // 'Name': escape($("#Name").val()),
'NickName':"dddd",
'Email':"moloic@163.com",
'CreateTime': "2015-12-12"
})
}
$.ajax({
type: "GET", //POST
url: "UserInfoHandler.ashx",
data: PostParam,
dataType: "json",
success: function (data) {
// alert("sss");
// debugger;
$('#resText').empty(); //清空resText里面的所有内容
var html = '';
$.each(data, function (commentIndex, comment) {
html += '<div class="comment"><h6>' + comment['Name']
+ ':</h6><p class="para"' + comment['Email']
+ '</p></div>';
});
$('#resText').html(html);
}
});
});
});
</script>
---------------------------------------------后台处理-----------------------------------------------------------------------------------
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication1
{
/// <summary>
/// UserInfoHandler 的摘要说明
/// </summary>
public class UserInfoHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
context.Response.Write(getUserList( context));
}
public bool IsReusable
{
get
{
return false;
}
}
public string getUserList(HttpContext context)
{
TestModel.UserInfo UserInfo = new TestModel.UserInfo();
//获取参数
UserInfo = (TestModel.UserInfo)JsonConvert.DeserializeObject(context.Request["UserInfo"].ToString(), typeof(TestModel.UserInfo));
UserInfo.Name= HttpUtility.UrlDecode(UserInfo.Name);
List<TestModel.UserInfo> userinfoList = new List<TestModel.UserInfo>();
//加载数据
try
{
ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();
IList<TestModel.UserInfo> ilist = new List<TestModel.UserInfo>();
ilist = service.getUserInfoData(UserInfo);
userinfoList = IListToList<TestModel.UserInfo>(ilist);
}
catch (Exception ex)
{
userinfoList = null;
}
return JsonConvert.SerializeObject(userinfoList);
}
/// <summary>
/// ILIST 转换LIST
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <returns></returns>
public List<T> IListToList<T>(IList<T> list)
{
T[] array = new T[list.Count];
list.CopyTo(array, 0);
return new List<T>(array);
}
}
}