JQuery在asp.net中三种ajax传值

1)通过webservice,注意去掉注释[System.Web.Script.Services.ScriptService]这行前的注释

2)通过aspx.cs文件中的静态方法

3)通过aspx文件url

WebForm1.aspx

WebForm1.aspx

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="asp.net.WebForm1" %>

<!DOCTYPE html 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 src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function Ws() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "WebService1.asmx/HelloWorld2",
data: "{name:‘xiaoxiao‘}",
dataType: ‘json‘,
success: function (result) {
alert(result.d);
}
});
}
function StaticMethod() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "aspxpage.aspx/SayHello2",
data: "{name:‘xiaoxiao‘}",
dataType: ‘json‘,
success: function (result) {
alert(result.d);
}
});

}
function FromPage() {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "dataContent.aspx?nowtime=‘" + new Date() + "‘",
data: "{}",
dataType: ‘html‘,
success: function (result) {
alert(result);
}
});

}

</script>
</head>
<body>
<form id="form1" runat="server">

<div>
<input id="Button1" type="button" value="jquery调用WebService" onclick="Ws()" />
</div>
<div>
<input id="Button2" type="button" value="jquery调用aspx页面静态方法" onclick="StaticMethod()" />
</div>
<div>
<input id="Button3" type="button" value="jquery通过page存储值" onclick="FromPage()" />
</div>
</form>
</body>
</html>

以上是启动页面,WebForm1.aspx.cs没有代码

WebService1.asmx

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace asp.net
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld()
{
return "Hello World"+System.DateTime.Now.ToLongTimeString();
}

[WebMethod]
public string HelloWorld2(string name)
{
return "Hello World" + name + System.DateTime.Now.ToLongTimeString();
}
}
}

以上是webservice中的代码

aspxpage.aspx.cs

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->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 asp.net
{
public partial class aspx页面代替ws : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
[WebMethod]
public static string SayHello()
{
return "Hello";
}

[WebMethod]
public static string SayHello2(string name)
{
return "Hello"+name;
}
}
}

以上是针对第二条 通过aspx.cs中的静态方法 注意方法前要加 [WebMethod],aspxpage.aspx页面没代码.

dataContent.aspx.cs

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace asp.net
{
public partial class dataContent : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Page.ViewStateMode = ViewStateMode.Disabled;
if (Request.QueryString["nowtime"] != null)
{
string stime = Request.QueryString["nowtime"].ToString();
Response.Write(stime);
}
Response.Flush();

}
}
}

以上是针对第三条 用url传值 通过aspx页面保存数据。dataContent.aspx页面没有代码.

时间: 2024-08-27 02:32:13

JQuery在asp.net中三种ajax传值的相关文章

jQuery 跨域访问的三种方式 No &#39;Access-Control-Allow-Origin&#39; header is present on the reque

问题: XMLHttpRequest cannot load http://v.xxx.com. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:63342' is therefore not allowed access. test.html:1 Resource interpreted as Script but transferred

sharepoint 判断用户是否存在某个组中三种方法

1.思路:查找用户所有的组来匹配是否在特定的组(推荐) 不用担心组不存在而报错. public static bool IsUserMemberOfGroup(SPUser user, string groupName) { bool result = false; if (!String.IsNullOrEmpty(groupName) && user != null) { foreach (SPGroup group in user.Groups) { if (group.Name =

ASP.NET中几种加密方法

下面就是ASP.NET中几种加密方法.加密算法有两种,也就是上面提到的MD5和SHA1,这里我举的例子是以MD5为例,SHA1大致相同,只是使用的类不一样. MD5的全称是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest开发出来,经md2.md3和md4发展而来.它的作用是让大容量信息在用数字签名软件签署私人密匙

unity中三种调用其他脚本函数的方法

第一种,被调用脚本函数为static类型,调用时直接用  脚本名.函数名().很不实用-- 第二种,GameObject.Find("脚本所在物体名").SendMessage("函数名");  此种方法可以调用public和private类型函数 第三种,GameObject.Find("脚本所在物体名").GetComponent<脚本名>().函数名();此种方法只可以调用public类型函数 unity中三种调用其他脚本函数的

VMware中三种网络连接的区别

VMware中三种网络连接的区别 1.概述 2.bridged(桥接模式) 3.NAT(网络地址转换模式) 4.host-only(主机模式) 5.replicate physical network connection state 选项解释 1.概述 大家在安装完虚拟机后,默认安装了两个虚拟网卡,VMnet1和VMnet8,如下图.其中VMnet1是host网卡,用于host方式连接网络的.VMnet8是NAT网卡,用于NAT方式连接网络的.它们的IP地址是随机生成的. VMware提供了三

文顶顶 iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

iOS开发UI篇—iOS开发中三种简单的动画设置

iOS开发UI篇—iOS开发中三种简单的动画设置 [在ios开发中,动画是廉价的] 一.首尾式动画 代码示例: // beginAnimations表示此后的代码要“参与到”动画中 [UIView beginAnimations:nil context:nil]; //设置动画时长 [UIView setAnimationDuration:2.0]; self.headImageView.bounds = rect; // commitAnimations,将beginAnimation之后的所

jQuery -&gt; 获取后代元素的三种方法

如果我们有内容如下的html文件,那么如何选取包含在<p>元素内的<i>元素呢? 邪馬台国の謎と弥生時代 紀元前1000年ごろ.水稲工作の技術をもつ集団が大挙して日本に移住してきた.これによって弥生時代が始まった. 縄文人も南方から伝わった稲を栽培していたが.彼らは原野を開いて大掛かりな水田をつくる発想を持たなかった.しかし.北九州に新たな文化を持って集団が来たのを知った彼らは次第に.水稲工作を受け入れ弥生人へとか分かっていた. html源码如下 <h1>邪馬台国の謎と

Android中三种超实用的滑屏方式汇总(转载)

Android中三种超实用的滑屏方式汇总 现如今主流的Android应用中,都少不了左右滑动滚屏这项功能,(貌似现在好多人使用智能机都习惯性的有事没事的左右滑屏,也不知道在干什么...嘿嘿),由于前段时间项目的需要,所以也对其研究了一下,总的来说滑屏实现有三种方式:(至于其他的实现方式目前后还没碰到...) 1.ViewPager 2.ViewFlipper 3.ViewFlow 一.ViewPager 官方文档介绍:http://developer.android.com/reference/