jquery异步调用页面后台方法‏(asp.net) - Web前端
作者:98wpeu发布时间:2026-05-31分类:网页前端技术浏览:73
导读:复制代码代码如下:<%@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 学习之一 对象访问 - Web前端
- 基于jQuery的左右滚动实现代码 - Web前端
- 突发奇想的一个jquery插件 - Web前端
- jQuery 表单验证扩展(三) - Web前端
- JQuery学习笔记 nt-child的使用 - Web前端
- JQuery UI DatePicker中z-index默认为1的解决办法 - Web前端
- Jquery跨域获得Json时invalid label错误的解决办法 - Web前端
- jquery picswitch图片焦点图展示效果 - Web前端
- jQuery 表单验证扩展(三) - Web前端
- 使用jquery与图片美化checkbox和radio控件的代码(打包下载) - Web前端
- 网页前端技术排行
-
- 1【第六章】Foundation之按钮和下拉功能 - Web前端
- 2jQuery编写widget的一些技巧分享 - Web前端
- 3在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 4基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 5分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 9JS网页制作实例:标签云 - 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属性


