zoukankan      html  css  js  c++  java
  • ajax post get

    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 = 23;
            }
    
            //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 == "1") {
                            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 == "123")
                {
                    context.Response.Write("1");
                }
                else
                {
                    context.Response.Write("Hello World");
                }
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
  • 相关阅读:
    Redis 如何与数据库事务保持一致
    QQ、微信 唯一登陆设计
    Node.js Sequelize如何实现数据库的读写分离
    node.js web应用优化之读写分离
    在docker中使用mysql数据库,在局域网访问
    docker常用管理命令
    基于 Egg.js 框架的 Node.js 服务构建之用户管理设计
    使用mousedown、mousemove、mouseup实现拖拽效果
    VUE中事件修饰符:stop prevent self capture
    基于jsplumb插件制作可拖拽、保存流程图、重绘保存后的流程图总结
  • 原文地址:https://www.cnblogs.com/hpbkin/p/6802962.html
Copyright © 2011-2022 走看看