net:不能使用jQuery调用Page WebMethod。

时间:2022-12-13 18:02:43

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

我在我页面的代码隐藏文件中创建了一个WebMethod:

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

I created the following HTML page to test it out:

我创建了下面的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>

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?

当我单击按钮时,AJAX会触发,但不会返回任何内容。在调试代码时,甚至不会调用method Test()。什么好主意吗?

5 个解决方案

#1


6  

try

试一试

url: "TestPage.aspx/Test"

or whatever relative url that will hit your page.

或任何相关的url,会击中你的页面。

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.

而且,虽然您还没有到达那里,但是您正在期待一个d:包装的对象。因为你只需要得到一个字符串。

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);
            }
        });
    }

#2


2  

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

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

#3


1  

I made this javascript function to call WebMethods using jQuery:

我用这个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  
    });
}

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

美元。json序列化由jquery.json-1.3插件实现。

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

可以看到,数据类型必须是"json"

#4


0  

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

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

#5


0  

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

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

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:

javascript应该在aspx页面加载完所有Microsoft特定库之后运行。当它们被加载时,你可以这样调用你的PageMethod:

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

OnSucceeded PageMethods.Test(函数(){ },()函数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

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.

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

getfirebug.com

getfirebug.com

#1


6  

try

试一试

url: "TestPage.aspx/Test"

or whatever relative url that will hit your page.

或任何相关的url,会击中你的页面。

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.

而且,虽然您还没有到达那里,但是您正在期待一个d:包装的对象。因为你只需要得到一个字符串。

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);
            }
        });
    }

#2


2  

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

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

#3


1  

I made this javascript function to call WebMethods using jQuery:

我用这个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  
    });
}

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

美元。json序列化由jquery.json-1.3插件实现。

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

可以看到,数据类型必须是"json"

#4


0  

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

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

#5


0  

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

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

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:

javascript应该在aspx页面加载完所有Microsoft特定库之后运行。当它们被加载时,你可以这样调用你的PageMethod:

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

OnSucceeded PageMethods.Test(函数(){ },()函数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

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.

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

getfirebug.com

getfirebug.com