jMsAjax 于Asp.Net的实现

jMsAjax Asp.Net的实现

Jiang,Zhiming

2008-08-09

binglingshui(a)gmail.com

 

1、关于jMsAjax

jMsAjax is a simple wrapper with a few enhancements for the $.ajax function built into jQuery to support ASP.net. The plugin supports both Web Methods and Web Services for both GET and POST requests.

jMsAjax是一套基于jQuery简单的包装插件,实现对ASP.NetAjax完整功能支持。插件同时实现了基于Web的方法和Web服务的GETPOST的请求。

 

2、下载:

http://schotime.net/jMsAjax.aspx

 

3、用前说明:

1,  首先,本框架基于.Net Framework 3.5

2.0里的命名空间里缺少

System.Web.Script.Services.ScriptMethod

2

4、使用方法:

4.1、新建项目:

4.2,添加jQueryjMsAjax的文件引用。

为了方便在文件中引用。我对下载了的文件改了名字。。

 

4.3、新建页面JMsAjax.aspx

添加引用:

本处省略。。。。。。。。。。。。。。。。

添加Js方法:

    $(function()

    {

        $("#btnAspx").click(function()

        {

            $.jmsajax({

            url: "JMsAjax.aspx",

            method: "GetTest",

            data:

            {

                uName: $("#userName").val(),

                uPassword: $("#userPassword").val()

            },

            success: function(data)

            {

                $("#div1").html(String(data));

            }

            });

        });

    });

 

附:官方给出的三种写法:

Defaults:

type: "POST"
data: {}
dataType: "msjson"
error: function to return the status and message

Basic Usage:

.jmsajax({ 
   url: "jMsAjax.aspx",
   method: "getTime",
   success: function(data) {
      $("#div").html(String(data));
   }
});

Advanced Usage:

.jmsajax({
   type: "POST",
   url: "jMsAjax.aspx",
   method: "getTime",
   dataType: "msjson",
   data: { date_in: new Date() },
   success: function(data) {
      $("#div").html(String(data));
   }
});

 

 

添加页面布局:

<div>

        <hr />

        username: <input id="userName" type="text" size="20" runat="server" /></br>

        password: <input id="userPassword" type="text" size="20" runat="server" /></br></br>

        <input id="btnAspx" type="button" value="调用页面方式" />

        <hr />

    </div>

    <div id="div">

    </div>

        <div id="div1">

</div>

 

注意,此处的控件应是runat="server"

 

附:方法的具体说明:

$("#btnAspx").click    我们在Js代码中为Button添加了一个Click事件的响应。。

 

url: "JMsAjax.aspx",    指的是要查找方法的文件名。

method: "GetTest",     要执行的方法名,在后台中必须为静态。

data:                   要进行传送的参数。

{uName: $("#userName").val(), uPassword: $("#userPassword").val()}

success: function(data) 后台执行成功后进行的处理,此处将数据显示在div1中的控件内

{$("#div1").html(String(data));}

 

4.4、添加后台对应代码:JMsAjax.aspx.cs

 [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]

[System.Web.Services.WebMethod]

public static string GetTest(string uName, string uPassword)

{

 return "Hello    " + uName + ":   " + uPassword;

}

 

5、注意事项:

1Framework的版本号。

2、多参数时的实参传递。

posted @ 2008-08-09 01:28  Binglingshui  阅读(1399)  评论(4)    收藏  举报
小明明