ajax 第二节jquery版

<!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;
            }
        }
    }
}
原文地址:https://www.cnblogs.com/xh0626/p/5005361.html