ASP .NET:无法使用 jQuery 调用 Page WebMethod

发布于 2024-08-31 11:10:30 字数 1019 浏览 2 评论 0原文

我在页面的代码隐藏文件中创建了一个 WebMethod,如下所示:

[System.Web.Services.WebMethod()]
public static string Test()
{
    return "TEST";
}

我创建了以下 HTML 页面来测试它:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/></script>
    <script type="text/javascript">
        function test() {            
            $.ajax({
                type: "POST",
                url: "http://localhost/TestApp/TestPage.aspx/Test",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "text",
                success: function(msg) {
                    alert(msg.d);
                }
            });
        }
    </script>
</head>
<body>
    <button onclick="test();">Click Me</button>
</body>
</html>

当我单击按钮时,AJAX 会触发,但不会返回任何内容。当我调试代码时,Test() 方法甚至不会被调用。有什么想法吗?

I created a WebMethod in the code-behind file of my page as such:

[System.Web.Services.WebMethod()]
public static string Test()
{
    return "TEST";
}

I created the following HTML page to test it out:

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"/></script>
    <script type="text/javascript">
        function test() {            
            $.ajax({
                type: "POST",
                url: "http://localhost/TestApp/TestPage.aspx/Test",
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "text",
                success: function(msg) {
                    alert(msg.d);
                }
            });
        }
    </script>
</head>
<body>
    <button onclick="test();">Click Me</button>
</body>
</html>

When I click the button, the AJAX fires off, but nothing is returned. When I debug my code, the method Test() doesn't even get called. Any ideas?

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

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

发布评论

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

评论(5

缪败 2024-09-07 11:10:30

尝试

url: "TestPage.aspx/Test"

或任何将访问您的页面的相对网址。

您可能无意中违反了同源政策

另外,尽管您还没有到达那里,但您正在期待 ad:wrapped 对象。事实上,你只会得到一根绳子。

这应该可以带你去你想去的地方。

    function test() {            
        $.ajax({
            type: "POST",
            url: "TestPage.aspx/Test",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert(msg.d);
            }
        });
    }

try

url: "TestPage.aspx/Test"

or whatever relative url that will hit your page.

You may be inadvertently violating same origin policy.

Also, although you are not there yet, you are expecting a d: wrapped object. As it is you are just going to get a string.

This should get you where you want to go.

    function test() {            
        $.ajax({
            type: "POST",
            url: "TestPage.aspx/Test",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                alert(msg.d);
            }
        });
    }
感情旳空白 2024-09-07 11:10:30

我认为数据类型应该是“json”。添加一个错误函数来查看返回的错误状态,即 404 未找到、500 服务器错误等

I think datatype should be "json". Add an error function to see what error status you get back ie 404 not found , 500 server error etc etc

撧情箌佬 2024-09-07 11:10:30

我制作了这个 javascript 函数来使用 jQuery 调用 WebMethods:

function pageMethod(fn, params, successFn, errorFn) {  
    var pagePath = window.location.pathname;  

    var jsonData = $.toJSON(params);

    $.ajax({  
        type: "POST",  
        url: pagePath + "/" + fn,  
        contentType: "application/json; charset=utf-8",  
        data: jsonData,  
        dataType: "json",  
        success: successFn,  
        error: errorFn  
    });
}

$.toJson 序列化是通过 jquery.json-1.3 插件实现的。

正如你所看到的,dataType 必须是“json”

I made this javascript function to call WebMethods using jQuery:

function pageMethod(fn, params, successFn, errorFn) {  
    var pagePath = window.location.pathname;  

    var jsonData = $.toJSON(params);

    $.ajax({  
        type: "POST",  
        url: pagePath + "/" + fn,  
        contentType: "application/json; charset=utf-8",  
        data: jsonData,  
        dataType: "json",  
        success: successFn,  
        error: errorFn  
    });
}

That $.toJson serialization is realized by jquery.json-1.3 plugin.

And as you can see, dataType must be "json"

青瓷清茶倾城歌 2024-09-07 11:10:30

您需要将 Test() 设置为接受/允许 POST

you need to set Test() to accept/allow POST

雪若未夕 2024-09-07 11:10:30

如果 PageMethods 在您的页面上正确注册,您应该能够使用 Microsoft 注册的名为 PageMethods 的对象来调用它们。

您的 javascript 应在 aspx 页面加载所有 Microsoft 特定库后运行。加载这些内容后,您可以通过以下方式调用 PageMethod:

PageMethods.Test(function() OnSucceeded{}, function() OnFailed{});

以下是更好示例的链接:

http://www.junasoftware.com/blog/using-jquery-ajax-and-page-methods-with-a-asp.net-webservice.aspx

如果您不是我已经强烈建议使用 Firebug 来帮助调试这些客户端调用。 Firebug 将为您提供确定实际情况所需的所有信息。

getfirebug.com

If the PageMethods are properly registered on your page, you should be able to call them with a Microsoft registered object called PageMethods.

Your javascript should run after the aspx page has loaded all the Microsoft specific libraries. When those are loaded, you could call your PageMethod this way:

PageMethods.Test(function() OnSucceeded{}, function() OnFailed{});

Here is a link to better examples:

http://www.junasoftware.com/blog/using-jquery-ajax-and-page-methods-with-a-asp.net-webservice.aspx

If you aren't already, I highly recommend using Firebug to help debug these client-side calls. Firebug will give you all the info you need to determine what is really going on.

getfirebug.com

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