zoukankan      html  css  js  c++  java
  • JQuery ($.get()) 前台传值到后台并调用后台方法

     
     

    前台JavaScript

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

        <script type="text/javascript" language="javascript">

         function TT()

         {

           var txtpost=document.getElementById("Text1").value;

           $.get("Default.aspx", { Action:"action", name: "John", time: "2pm" },

              function(data){  //此处是回调函数 接收从后台传回的值

               alert(data);  //接回来的值是一串字符串

           });

         }

        </script>

     

    C# 后台方法

     protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                ajax();

            }

        }

     

        private void ajax()

        {

            string action = Request["Action"];  //最得前台的JS的第一个参数

            if (!string.IsNullOrEmpty(action) && action == "action")  //判断是否通过前台的点击事件进来的

            {

                Response.Write("你从前台JS里传入的是:" + Request["name"].ToString() + "和" + Request["time"].ToString());

                Response.End();

            }

        }

    补充啊:以下2句是相等的

    1、

    $.get("Handler.ashx", { "name": "haha", "id": 321 }, function (data) { alert(data); })

    2、

    $.ajax({
    url: "Handler.ashx",
    data: { "name": "haha", "id": 321 },
    success: (function (data) {
    alert(data);
    })
    });

  • 相关阅读:
    linux 命令——19 find (转)
    linux 命令——18 locate (转)
    linux 命令——17 whereis(转)
    linux 命令——16 which(转)
    linux 命令——15 tail (转)
    linux 命令——14 head (转)
    Java for LeetCode 038 Count and Say
    Java for LeetCode 037 Sudoku Solver
    Java for LeetCode 036 Valid Sudoku
    Java for LeetCode 035 Search Insert Position
  • 原文地址:https://www.cnblogs.com/wdw31210/p/2323903.html
Copyright © 2011-2022 走看看