Ajax 方法调用 [英] Ajax method call

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

问题描述

我试图在我的代码中使用 Jquery 和 Ajax 调用一个简单的方法.但是我每次都会收到 404 not found 异常.不幸的是,这是一个网络表单解决方案.所以我没有 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# 方法.我之前使用这个 Jquery 方法的经验是在一个 MVC 网站上.它与网络表单网站兼容吗?

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# 代码是:

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

    return ret;
}

提前致谢.

推荐答案

你的方法返回 JsonResult.这是特定于 MVC 的,您不能在 webforms 应用程序中使用它.

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

如果您想在经典的 WebForms 应用程序中调用隐藏代码中的方法,您可以使用 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();
}

然后调用方法:

$.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天全站免登陆