zoukankan      html  css  js  c++  java
  • asp.net利用Ajax和Jquery在前台向后台传参数并返回值

    1----------前台

    首先需要 Jquer的包

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

    下面是
        <script type="text/javascript">
            $(function () {
                $('#txtUserName').blur(function () {
                    var username = $(this).val();
                    $.ajax({
                        type: "post",
                        contentType: "application/json",//传值的方式
                        url: "WebAjaxForMe.aspx/GetValueAjax",//WebAjaxForMe.aspx为目标文件,GetValueAjax为目标文件中的方法
                        data: "{username:'" + username + "'}",//username 为想问后台传的参数(这里的参数可有可无)
                        success: function (result) {
                            alert(result.d);//result.d为后台返回的参数
                        }
                    })
                })
            }) 
        </script>
     
    这里是参数的来源
            <input id="txtUserName" type="text" />

    2------------后台

    在后台首先要添加using System.Web.Services;的引用


     [WebMethod]//方法前边必须添加 [WebMethod]
            public static string GetValueAjax(string username)//这个方法需要是静态的方法要用到关键字static
            {

                //在这里可以对传进来的参数进行任何操作
                return username;
            } 
     

  • 相关阅读:
    ucore 物理内存探测 lab2 附录A&B
    git diff 笔记
    操作系统Lab1 详解(boot|kern/debug)
    ucore os 前初始化
    第五讲 计算机体系结构 内存层次
    Django 的学习(1) 从建立到数据库操作
    ucore os 初始化
    操作系统 Lab1
    makefile 语法笔记 3
    flex布局注意事项
  • 原文地址:https://www.cnblogs.com/liupengfei19940119/p/3994916.html
Copyright © 2011-2022 走看看