方法一:
第一步,建立一个.aspx文件
在查看其源代码里写:即.aspx.cs,主要是使用using System.Web.Services;
关键处:[WebMethod]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string HandleEvent01()
{
//code...
return "YOUR DATA";
}
}
在.aspx文件里写:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
type: "POST",
contentType: "application/json",
url: "Default2.aspx/HandleEvent01", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
dataType: 'json', //WebService 返回Json类型 或者 Json/string
success: function(re) {
$('#re_method04').html(re.d);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="re_method04">
</div>
</form>
</body>
</html>
方法二:
建立一个Handler.ashx。类作为后台处理请求:
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
Json j = new Json();
j.Msg = "试验成功了,哈哈哈哈!";
context.Response.Write(MyJsonConvert.SerializeObjectEx(j));
}
public bool IsReusable {
get {
return false;
}
}
}
前台访问:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "POST",
contentType: "application/json",
url: "Handler.ashx/ProcessRequest", //调用WebService的地址和方法名称组合 ---- WsURL/方法名
dataType: 'json', //WebService 返回Json类型 或者 Json/string
success: function (re) {
console.info(re);
$('#re_method04').html(re.Msg);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="re_method04">
</div>
</form>
</body>
</html>
序列化JSON使用的是Newtonsoft:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
/// <summary>
///MyJsonConvert
/// 序列化对象为JSON
/// </summary>
public class MyJsonConvert
{
public MyJsonConvert()
{
}
public static string SerializeObjectEx(object value)
{
Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter(); //这里使用自定义日期格式,默认是ISO8601格式
timeConverter.DateTimeFormat = "yyyy-MM-dd"; //设置时间格式
Newtonsoft.Json.Converters.StringEnumConverter enumConverter = new Newtonsoft.Json.Converters.StringEnumConverter(); //枚举值转换成字符串名字
JsonSerializerSettings JsonSetting = new JsonSerializerSettings();
JsonSetting.Converters.Add(timeConverter);
JsonSetting.Converters.Add(enumConverter);
JsonConvert.SerializeObject(value, JsonSetting);
return JsonConvert.SerializeObject(value,JsonSetting);
}
}
待序列化的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
///Json
/// 返回json数据
/// </summary>
public class Json
{
private string msg;
public string Msg
{
get { return msg; }
set { msg = value; }
}
public Json()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
}
结果: