1.Ajax   post 方法

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AJAXtest.aspx.cs" Inherits="treeview.AJAXtest" %>

<!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 id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ajax实例1</title>
<script src="js/jquery-1.4.4.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btn1").click(function () {
var txtparam1 = $("#txtParam1").val();
var txtparam2 = $("#txtParam2").val(); $.ajax({
url: "RepeatName.ashx",//发送到本页面后台AjaxMethod方法 "AJAXtest.aspx/AjaxMethod"
type: "POST",
dataType: "json",//返回格式必须是json格式,否则会进error方法,如果去掉可以接受任意的格式数据。
            //后台如果传自定义类 的数据 该如何接收? 后台需要用newtonsoft把类转换成json数据 返回。然后前端在success里直接 data.id即可。
async: true,//async翻译为异步的,false表示同步,会等待执行完成,true为异步
contentType: "application/json; charset=utf-8",//不可少
data: "{param1:'" + txtparam1 + "',param2:'" + txtparam2 + "'}",
success: function (data) {
$("#result").html(data.d);
},
error: function () {
alert("请求出错处理");
}
}); });
});
</script>
</head>
<body>
<form id="form1" runat="server">
参数1:<input type="text" id="txtParam1" value="" /><br />
参数2:<input type="text" id="txtParam2" value="" /><br />
<input type="button" id="btn1" value="提交"/><br />
<div id="result"></div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq; namespace treeview
{
public partial class AJAXtest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int dfe = ;
} //type方式必须是post,方法必须是静态的,方法声明要加上特性[System.Web.Services.WebMethod()],传递的参数个数也应该和方法的参数相同。
[System.Web.Services.WebMethod()]
public static string AjaxMethod(string param1, string param2)
{
return "1参数为:" + param1 + ",2参数为:" + param2;
} }
}

2. Ajax  get

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ajaxGet.aspx.cs" Inherits="treeview.ajaxGet" %>

<!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 id="Head1" runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Ajax实例1</title>
<script src="js/jquery-1.4.4.min.js"></script>
<script language="Javascript">
function RepeatName() {
var name = $("#txtLoginName").val();
$.ajax({
type: "GET",
url: "RepeatName.ashx",
data: "name=" + name, //如果这里是传多个参数的话 可以写成 date: "name="+name+"&"+"其它的名字="+页面获取的val();
cache: false,
async: false,
success: function (data) { if (data == "") {
alert("此用户已经注册");
}
else{
alert("没有注册");
}
},
error: function () {
alert("请求出错处理");
}
});
}
</script>
</head>
<body>
<div>
<input type="text" name="txtLoginName" id="txtLoginName" onblur="return RepeatName()" />
<span style="color: red; font-size: 13px" id="txtLoginNamespan"> </span>
</div>
</body>
</html>
using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq; namespace treeview
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class RepeatName : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World"); string name = context.Request.QueryString["param1"];
if (name == "")
{
context.Response.Write("");
}
else
{
context.Response.Write("Hello World");
}
} public bool IsReusable
{
get
{
return false;
}
}
}
}

最新文章

  1. Marmoset Toolbag中的角色布光技巧 by Joe”EarthQuake”Wilson
  2. 数据库设计范式1&mdash;&mdash;三范式
  3. Ajax (一)
  4. UIView Programming Guide学习笔记
  5. Less tips:声明变量之前可以引用变量!
  6. JDK的下载和安装
  7. mysql5.7慢查询开启配置
  8. Android开源项目大全 - 工具类
  9. 【转】50条大牛C++编程开发学习建议
  10. linux下mysql重置密码
  11. Java学习记录 : 画板的实现
  12. The 16th tip of DB Query Analyzer
  13. 译MassTransit 消息契约
  14. idea在Terminal中使用maven指令
  15. json对象转对象
  16. C语言闰年问题程序框图
  17. Ubuntu14.04下中文输入法拼音不正常问题 输入nihao会变成niha o
  18. Mac 开发使用中的小技巧收集
  19. 分析轮子(八)- List.java 各种遍历方式及遍历时移除元素的方法
  20. angular中使用ckplayer播放器

热门文章

  1. Java 8 – MinguoDate examples
  2. [ci]jenkins构建容器项目java-helloworld-非docker plugin模式
  3. Java 汇编代码
  4. easy_install与pip 区别
  5. 【Spark 深入学习 -09】Spark生态组件及Master节点HA
  6. 在webpack中使用postcss-px2rem的
  7. 大津法---OTSU算法
  8. cannot open shared object file: No such file or directory
  9. An SPI class of type org.apache.lucene.codecs.PostingsFormat with name &#39;Lucene50&#39; does not exist. You need to add the corresponding JAR file supporting this SPI to your classpath. The current classp
  10. Java如何查找系统的代理设置?