您的位置:首页 > Web前端 > JQuery

ajax 第二节jquery版

2015-11-29 20:29 666 查看
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>

<script src="js/jquery-1.8.2.min.js"></script>
<script type="text/javascript">

$(function () {

$("#btn").click(
function () {

if (XMLHttpRequest) {

var xhr = new XMLHttpRequest();

}
else {

//ie  5 6
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

xhr.open("get", "handler1.ashx?name=1", true);
xhr.send();

xhr.onreadystatechange = function () {

if (xhr.readyState == 4) {

if (xhr.status == 200) {

$("#test1").val(xhr.responseText);
}

}

}

}

);

$("#btn1").click(
function () {

//jQuery.get(url, [data], [callback], [type])
//参数 标注  :  url,参数 {键值对},回调函数,返回内容格式 : xml, html, script, json, text, _default

$.get("handler1.ashx?name=1", {}, function (data) { $("#test1").val(data); },"text")

}

);

$("#btn2").click(
function () {
// 参考get

$.post("handler1.ashx", { "name": "1" }, function (data) { $("#test1").val(data); },"text")

}

);

$("#btn3").click(
function () {
// 这种写法 传递的参数都在 data

//1.文本:"uname=alice&mobileIpt=110&birthday=1983-05-12"

//2.json对象:{uanme:'vic',mobileIpt:'110',birthday:'2013-11-11'}

//3.json数组:
// [
//    {"name":"uname","value":"alice"},
//    {"name":"mobileIpt","value":"110"},
//    {"name":"birthday","value":"2012-11-11"}
//]

$.ajax( {
type : "GET",
url: "handler1.ashx",
data :"name=1&name=2",

success : function(msg){

$("#test1").val(msg);
}

})

}

);

})

</script>
</head>
<body>

<input type="button" id="btn" value="ajax传统写法" />
<input type="button" id="btn1" value="ajax--jquery写法get" />
<input type="button" id="btn2" value="ajax--jquery写法post" />
<input type="button" id="btn3" value="ajax--jquery写法options" />

<input type="text" id="test1" value="3" />

</body>
</html>


一般处理程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2
{
/// <summary>
/// Handler1 的摘要说明
/// </summary>
public class Handler1 : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{

string a =  context.Request["name"].ToString();
context.Response.ContentType = "text/plain";
context.Response.Write(a);
}

public bool IsReusable
{
get
{
return false;
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: