asp.net利用Ajax和Jquery在前台向后台传参数并返回值的实例

所属分类: 网络编程 / ASP.NET 阅读数: 883
收藏 0 赞 0 分享

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;    

 }

更多精彩内容其他人还在看

用ASP.Net实现文件的在线压缩和解压缩

用ASP.Net实现文件的在线压缩和解压缩
收藏 0 赞 0 分享

ASP.NET中文件上传下载方法集合

ASP.NET中文件上传下载方法集合
收藏 0 赞 0 分享

ASP.NET通过Remoting service上传文件

ASP.NET通过Remoting service上传文件
收藏 0 赞 0 分享

ASP.NET2.0服务器控件之Render方法

ASP.NET2.0服务器控件之Render方法
收藏 0 赞 0 分享

ASP.NET2.0 WebRource,开发微调按钮控件

ASP.NET2.0 WebRource,开发微调按钮控件
收藏 0 赞 0 分享

ASP.NET2.0新特性概述

ASP.NET2.0新特性概述
收藏 0 赞 0 分享

介绍几个ASP.NET中容易忽略但却很重要的方法函数

介绍几个ASP.NET中容易忽略但却很重要的方法函数
收藏 0 赞 0 分享

asp.net2.0如何加密数据库联接字符串

asp.net2.0如何加密数据库联接字符串
收藏 0 赞 0 分享

用.NET 2.0压缩/解压功能处理大型数据

用.NET 2.0压缩/解压功能处理大型数据
收藏 0 赞 0 分享

ASP.NET入门随想之检票的老太太

ASP.NET入门随想之检票的老太太
收藏 0 赞 0 分享
查看更多