Ajax的方法调用 [英] Ajax method call

查看:165
本文介绍了Ajax的方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图调用一个简单的方法,在我的code使用jQuery使用Ajax背后。但我得到一个404未发现异常每次。不幸的是,这是一个Web表单解决方案。所以,我没有MVC的所有津贴:(

I am trying to call a simple method in my code behind using Jquery with Ajax. But I get a 404 not found exception everytime. Unfortunately this is a web forms solution. So I dont have all the perks of MVC :(

它进入JavaScript方法,并给出了警告,但不会进入我的C#方法。我的previous使用这个jQuery方法的经验,是一个MVC的网站。它是用的WebForms网站兼容?

It does get into the javascript method and gives the alert but won't go into my c# method. My previous experience of using this Jquery method is in an MVC website. Is it compatible with webforms sites?

我的Javascript是:

My Javascript is:

$(document).ready(function() {

              $('#btn_<%=UserStuff.tag %>').click(function() {                    

                  var value = $('#<%#Eval("tag") %>twink').val();
                  something(value);                    
              });
          });


          function something(theval) {

            alert(theval);

              $.ajax({
                  type: "POST",
                  url: "/Default.aspx/MyMethod?something=" + theval,
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function(msg) {
                      alert(msg);
                  }
              });
          }
}

和我的C#code是:

And my C# code is:

   public JsonResult MyMethod(string something)
{
    JsonResult ret = new JsonResult();      

    return ret;
}

在此先感谢。

Thanks in advance.

推荐答案

您方法的返回值 JsonResult 。这是MVC具体的,你不能在一个web表单应用程序中使用它。

Your method returns JsonResult. This is MVC specific and you cannot use it in a webforms application.

如果你想调用在code方法落后于经典的WebForms应用程序,你可以使用<一个href="http://encosia.com/2008/05/29/using-jquery-to-directly-call-aspnet-ajax-page-methods/">PageMethods:

If you want to call methods in the code behind in a classic WebForms application you could use PageMethods:

[WebMethod]
public static string GetDate()
{
    return DateTime.Now.ToString();
}

然后调用方法:

And then to call the method:

$.ajax({
    type: 'POST',
    url: 'PageName.aspx/GetDate',
    data: '{ }',
    contentType: 'application/json; charset=utf-8',
    dataType: 'json',
    success: function(msg) {
        // Do something interesting here.
    }
});

这是一个完整的工作的例子,我为你写的:

And here's a full working example I wrote for you:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>
<script type="text/C#" runat="server">
    [WebMethod]
    public static string SayHello(string name)
    {
        return "Hello " + name;
    }
</script>
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="/scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: 'POST',
                url: 'default.aspx/sayhello',
                data: JSON.stringify({ name: 'John' }),
                contentType: 'application/json; charset=utf-8',
                dataType: 'json',
                success: function (msg) {
                    // Notice that msg.d is used to retrieve the result object
                    alert(msg.d);
                }
            });
        });
    </script>
</head>
<body>
    <form id="Form1" runat="server">

    </form>
</body>
</html>

PageMethods不限于简单的参数类型。可以使用任何类型的输入和输出,这将是自动JSON序列

PageMethods are not limited to simple argument types. You could use any type as input and output, it will be automatically JSON serialized.

这篇关于Ajax的方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆