AJAX WebMethod 帮助

发布于 2024-10-02 13:44:15 字数 1144 浏览 1 评论 0原文

我一直在尝试从 jQuery AJAX 访问 .NET WebMethod,但无法让它工作。我已经阅读了在 SO 和其他网站上找到的所有内容,但似乎没有任何效果。

我的 C# 代码是这样的

[WebMethod]
public static string TestAjax()
{
    return "Hello World";
}

,JavaScript 代码是

$.ajax({
    type: "POST",
    url: "ManageEvent.aspx/TestAjax",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg)
    {
        alert(msg.d);
    },
    error: function(result)
    {
        alert("error: " + result.status);
    }
});

最终目标是使用 AJAX 向数据库提交表单,而无需重新加载整个页面,但目前我什至无法让它返回一个字符串。当我单击附加此代码的输入按钮时,错误代码被称为给出状态 12030。但是当我在 Visual Studio 之外测试代码时,我仍然出错,但状态为 200。我的代码中是否有任何明显的明显错误?会导致此错误的代码?

非常感谢

编辑:

通过安装 ASP.NET AJAX 1.0 并在我的 Javascript 中调用

PageMethods.TextAjax(OnSuccess, OnFail);

,其中 OnSuccess 和 OnFail 是函数。但是我仍然无法让 jQuery 的 AJAX 工作。它仍然抛出 12030 错误状态。

I have been trying to access a .NET WebMethod from jQuery AJAX but I cannot get it to work. I have read everything I can find on SO and other sites and nothing seems to be working.

My C# code is such

[WebMethod]
public static string TestAjax()
{
    return "Hello World";
}

and the JavaScript code is

$.ajax({
    type: "POST",
    url: "ManageEvent.aspx/TestAjax",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg)
    {
        alert(msg.d);
    },
    error: function(result)
    {
        alert("error: " + result.status);
    }
});

The end goal was to do a form submission to the database using AJAX without reloading the entire page but at the moment I can't even get it to return back a String. When I click the input button to which this code is attached the error code is called giving a status of 12030. But when I test the code outside of Visual Studio I still error but with a status of 200. Is there anything blatantly obvious in my code that would cause this error?

Thanks a ton

EDIT:

I have gotten the WebMethod to work using ASP.NET Ajax's ScriptManager by installing ASP.NET AJAX 1.0 and calling in my Javascript

PageMethods.TextAjax(OnSuccess, OnFail);

where OnSuccess and OnFail are functions. However I still cannot get jQuery's AJAX to work. It still throws the 12030 error status.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事和酒 2024-10-09 13:44:15

我看不出你的代码有什么问题。安装 FireBug 并查看您的设置问题出在哪里。这是一个完整的工作示例:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>

<script type="text/C#" runat="server">
    [WebMethod]
    public static string TestAjax()
    {
        return "Hello World";
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $.ajax({
            type: "POST",
            url: "Default.aspx/TestAjax",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (result) {
                alert("error: " + result.status);
            }
        });
    </script>
</head>
<body>
    <form id="Form1" runat="server">
    </form>
</body>
</html>

I can't see anything wrong with your code. Install FireBug and see where's the problem with your setup. Here's a full working example:

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Services" %>

<script type="text/C#" runat="server">
    [WebMethod]
    public static string TestAjax()
    {
        return "Hello World";
    }
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head id="Head1" runat="server">
    <title></title>
    <script type="text/javascript" src="scripts/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $.ajax({
            type: "POST",
            url: "Default.aspx/TestAjax",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                alert(msg.d);
            },
            error: function (result) {
                alert("error: " + result.status);
            }
        });
    </script>
</head>
<body>
    <form id="Form1" runat="server">
    </form>
</body>
</html>
南城追梦 2024-10-09 13:44:15

这一切看起来都是正确的。你的班级是不是这样:

public partial class _Default : Page 
{
[WebMethod]
public static string TestAjax()
{
    return "Hello World";
}

}

That all looks correct. Does your class look like this:

public partial class _Default : Page 
{
[WebMethod]
public static string TestAjax()
{
    return "Hello World";
}

}
内心旳酸楚 2024-10-09 13:44:15

Web 服务缺少 [ScriptService] 标记,该标记在安装 ASP.NET AJAX 后修复了该问题

The Web service was missing a [ScriptService] tag which fixed it after installing ASP.NET AJAX

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文