zoukankan      html  css  js  c++  java
  • Javascript呼叫.axd文档

    axd文档与ashx文档有相似的功能。此博文中,Insus.NET演示如何在Javascript呼叫到axd文档。能呼叫到axd文档,当然也可以呼叫到ashx的,不过此次axd是主角。

    在你的专案的App_Code中,创建一个类别,记得要实作IHttpHandler接口。 

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

    /// <summary>
    /// Summary description for InsusClass
    /// </summary>
    namespace Insus.NET
    {
        public class InsusClass : IHttpHandler
        {
            public InsusClass()
            {
                //
                
    // TODO: Add constructor logic here
                
    //
            }

            public bool IsReusable
            {
                get { throw new NotImplementedException(); }
            }

            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "text/Plain";
                string parm = context.Request.Params["v"];
                context.Response.Write("Hello, " + parm);        
            }
        }
    }

     然后,在web.config注册axd文档:

    为了做一个演示,我们可以在.aspx放置一个TextBox和一个Button。这样用户可以在文本框中输入文字,点击铵钮可以呼叫到它。

    View Code
     <asp:TextBox ID="TextBoxName" runat="server"></asp:TextBox>
                <asp:Button ID="ButtonCall" runat="server" Text="Call"
                    OnClientClick
    ="callAxd();" />

    在铵钮中使用了OnClientClick="callAxd();", 是客户端执行,可以直接Ctrl + C,Ctrl + V帖于head 即可。

    View Code
    function callAxd() {
                var xhr = new XMLHttpRequest();

                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4 && xhr.status == 200) {
                        alert(xhr.responseText);
                    }
                }

                var url = "i.axd?v=" + document.getElementById('<%=TextBoxName.ClientID%>').value;
                xhr.open("GET", url, true);
                xhr.send();
            }

    OK,我们运行一下:

  • 相关阅读:
    一个很实用的css3兼容工具很多属性可以兼容到IE6
    html5 canvas 填充渐变形状
    找到任何形状的中心-总结篇
    html canvas非正方旋转和缩放...写的大多是正方的有人表示一直看正方的看厌了
    把jQuery的类、插件封装成seajs的模块的方法
    那些年实用但被我忘掉javascript属性.onresize
    总有一些实用javascript的元素被人遗忘在角落-slice
    jquery(入门篇)无缝滚动
    html5 canvas旋转+缩放
    今天看到这篇新闻之后,决定休息一下咯
  • 原文地址:https://www.cnblogs.com/insus/p/2766488.html
Copyright © 2011-2022 走看看