jquery异步调用页面后台方法‏(asp.net) - Web前端
作者:98wpeu发布时间:2026-05-31分类:网页前端技术浏览:3
导读:复制代码代码如下:<%@PageLanguage="c#"AutoeventWireup="true"codeBehind="JQueryCSMethodFORM....
复制代码 代码如下:
<%@ Page Language="c#" AutoeventWireup="true" codeBehind="JQueryCSMethodFORM.aspx.cs" Inherits="jquerWEB.jQueryCSMethodform" %>
<!DOCtypehtml PUBLIC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xHTML1/DTD/xhtml1-transitional.dtd">
<html xmlns="HTTP://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="JS/jquery-1.4.2.js"></script>
<script type="text/JavaScript">
$(document).ready(function() {
$("input[type='button'][value='getdate']").click(function() {
$.Ajax({
type: "post",
url: "JQueryCSMethodForm.aspx/GetNowDate",
datatype: "json",
contentType: "APPlication/JSON; charset=utf-8",
success: function(data) {
$("input#showtime").val(eval('(' + data.d + ')')[0].nowtime);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
$("input[type='button'][value='GetOneDayLater']").click(function() {
$.ajax({
type: "post",
url: "JqueryCSMethodForm.aspx/GetOneDayLate",
data:"{days:1}",
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
$("input#showTime").val(eval('(' + data.d + ')')[0].nowtime);
},
error: function(XMLHttPRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="GetDate" />
<input type=button value="GetOneDayLater" />
<input type="text" id="showTime" />
</div>
</form>
</body>
</html>
CS代码:
复制代码 代码如下:
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;
namespace JQuerWeb
{
public partial class JqueryCSMethodForm : System.Web.UI.Page
{
protected void Page_load(Object sender, EventArgs e)
{
}
[WebMethod]
public static string GetNowDate()
{
return "[{\"nowtime\":\"" + DateTime.Now.ToShortDateString() + "\"}]";
}
[WebMethod]
public static String GetOneDayLate(int32 days)
{
return "[{\"nowtime\":\"" + DateTime.Now.AddDays(days).ToShortDateString() + "\"}]";
}
}
}
注意点:
(1) url的填写格式 url+"/method name"
(2) contentType: "Application/json; charset=utf-8", 这个必须要有
(3) 返回数据的类型为json
(4) data:"{days:1}",参数的传递
(5) 后台的方法必须是public static 而且还要有 [WebMethod]特性修饰
<%@ Page Language="c#" AutoeventWireup="true" codeBehind="JQueryCSMethodFORM.aspx.cs" Inherits="jquerWEB.jQueryCSMethodform" %>
<!DOCtypehtml PUBLIC "-//W3C//DTD xhtml 1.0 Transitional//EN" "http://www.w3.org/TR/xHTML1/DTD/xhtml1-transitional.dtd">
<html xmlns="HTTP://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript" src="JS/jquery-1.4.2.js"></script>
<script type="text/JavaScript">
$(document).ready(function() {
$("input[type='button'][value='getdate']").click(function() {
$.Ajax({
type: "post",
url: "JQueryCSMethodForm.aspx/GetNowDate",
datatype: "json",
contentType: "APPlication/JSON; charset=utf-8",
success: function(data) {
$("input#showtime").val(eval('(' + data.d + ')')[0].nowtime);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
$("input[type='button'][value='GetOneDayLater']").click(function() {
$.ajax({
type: "post",
url: "JqueryCSMethodForm.aspx/GetOneDayLate",
data:"{days:1}",
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
$("input#showTime").val(eval('(' + data.d + ')')[0].nowtime);
},
error: function(XMLHttPRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="button" value="GetDate" />
<input type=button value="GetOneDayLater" />
<input type="text" id="showTime" />
</div>
</form>
</body>
</html>
CS代码:
复制代码 代码如下:
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;
namespace JQuerWeb
{
public partial class JqueryCSMethodForm : System.Web.UI.Page
{
protected void Page_load(Object sender, EventArgs e)
{
}
[WebMethod]
public static string GetNowDate()
{
return "[{\"nowtime\":\"" + DateTime.Now.ToShortDateString() + "\"}]";
}
[WebMethod]
public static String GetOneDayLate(int32 days)
{
return "[{\"nowtime\":\"" + DateTime.Now.AddDays(days).ToShortDateString() + "\"}]";
}
}
}
注意点:
(1) url的填写格式 url+"/method name"
(2) contentType: "Application/json; charset=utf-8", 这个必须要有
(3) 返回数据的类型为json
(4) data:"{days:1}",参数的传递
(5) 后台的方法必须是public static 而且还要有 [WebMethod]特性修饰
相关推荐
- jQuery 1.5 源码解读 面向中高阶JSER - Web前端
- 为jQuery增加join方法的实现代码 - Web前端
- jquery 操作表格实现代码(多种操作打包) - Web前端
- Jquery知识点一 Jquery的ready和Dom的onload的区别 - Web前端
- 扩展easyui.datagrid,添加数据loading遮罩效果代码 - Web前端
- 使用jQuery.fn自定义jQuery翻页插件 - Web前端
- jQuery的实现原理的模拟代码 -2 数据部分 - Web前端
- 基于jQuery的的一个隔行变色,鼠标移动变色的小插件 - Web前端
- jQuery编写widget的一些技巧分享 - Web前端
- niceTitle 基于jquery的超链接提示插件 - Web前端
- 网页前端技术排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


