首页 新闻 赞助 找找看

ASP.NET AJAX调用本页面方法怎么实现?

0
悬赏园豆:200 [已解决问题] 解决于 2013-04-11 08:36

我用的是.NET FRAMEWORK 2.0 ,在网上找了个例子,但是不行,跪求指导。

 

后台代码:

        [WebMethod]
        public static string getString()
        {
            return "This is JQuery";
        }

        [WebMethod]
        public static string getName(string name, int id)
        {
            return name + "_" + id;
        }

前台代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="jquery-1.4.2.min.js" language="javascript" type="text/javascript"></script>
    <script type="text/javascript">
        var ajaxType = "Post";
        var ajaxAsync = false;
        var ajaxContentType = "application/json; charset=utf8";
        var ajaxDataType = "json";

        function getValueByAjax(requestUrl, paramsString) {
            var strRetValue = "";
            if (paramsString != null)
                paramsString = "{" + paramsString + "}";
            $.ajax({
                type: ajaxType,
                async: ajaxAsync,
                url: requestUrl,
                contentType: ajaxContentType,
                dataType: ajaxDataType,
                data: paramsString,
                success: function(data) {
                        strRetValue = data.d;  
                },
                error: function(err) {
                    strRetValue = err;
                }

               error:function(request,msg,errObj){    
                 alert(msg);    
               }

            });
            return String(strRetValue);
        }

        function alertString() {
            var str = getValueByAjax("WebForm1.aspx/getString");
            alert(str);
            var name = getValueByAjax("WebForm1.aspx/getName", "name:'Jony', id:" + 123);
            alert(name);
        }
       
    
       
    </script>
</head>
<body>
<input type="button" value="获取信息" onclick="alertString();" />
</body>
</html>

五行缺木的主页 五行缺木 | 菜鸟二级 | 园豆:386
提问于:2013-04-10 20:15
< >
分享
最佳答案
1

路径有没有问题啊,绝对路径试试……

另外有人这样说:

//后台代码
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
//前台代码
$.ajax({
type: "post",
datatype:"json",
url: "ajax.aspx/Hello",
contentType: "application/json",
success: function (result) {
alert(result.d);
}
});
 
 .net 3.5可以直接调用,如果是.net 2.0的话.要在web.config中的 的 <system.web> 节点下添加
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
以上代码经过本人测试可行
收获园豆:200
滴答的雨 | 老鸟四级 |园豆:3681 | 2013-04-10 20:42

       public static string getName(string name, int id)

zhi++ | 园豆:487 (菜鸟二级) | 2013-04-10 20:51
其他回答(3)
1

帮顶

zhi++ | 园豆:487 (菜鸟二级) | 2013-04-10 20:29
0

和asp.net ajax没有关系吧,你用的jquery ajax函数,路径也不应该那样写,那样写是对应不到某个静态方法。要用asp.net的现引用scriptmanage。

Astar | 园豆:40805 (高人七级) | 2013-04-10 21:28

要post过去

支持(0) 反对(0) Astar | 园豆:40805 (高人七级) | 2013-04-10 21:33
0
 function CallServerFunction(url) {

            $.ajax({
                type: "post",
                url: url,
                contentType: "application/json; charset=utf-8",
                data: "{str:'Hello world!'}",
                dataType: "json",
                success: function (result) {

                    alert(result.d);

                },
                error: function (result) {
                },
                async: true
            });
        }

CallServerFunction("Default.aspx/getHello");

==================================
后台:

[System.Web.Services.WebMethod()]
public static string getHello(string str)
{
            return str;
}


==============

你上面的前台代码:两个error之间少了个 ,

[WebMethod] ==> [System.Web.Services.WebMethod()]

Yu | 园豆:12980 (专家六级) | 2013-04-10 21:29

帅哥,你上面的代码试验过么?我试了一下貌似不行啊,alert 结果是 undefined,   额~~~  我的意思是 如果我只获取数据是没有问题的,但是这种方法好像不能传参数给服务器端吧。

支持(0) 反对(0) 动感超人z | 园豆:1 (初学一级) | 2013-04-11 09:46

@枼秋: 如果是.net 2.0的话,按一楼说的,要添加

<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
支持(0) 反对(0) Yu | 园豆:12980 (专家六级) | 2013-04-11 12:58
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册