1

I have a function like this

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

I want to call it in my aspx page. so this is what I am trying

    function sendData(){
        $.post("Default.aspx/Hello", function(data){
            alert(data);
        }).fail(function() {
            alert( "error" );
        });
    }

Now caling this is successful and doesn't return error, but it doesn't return what I want. Instead of returning the string "hello" it gives me back a string of the html of the page

0

3 Answers 3

1

You need to use data.d:

function sendData(){
        $.post("Default.aspx/Hello", function(data){
            alert(data.d);
        }).fail(function() {
            alert( "error" );
        });
    }

Dave Ward article on why you need to use .d

You also need to make sure that you have added a script manager and EnablePageMethods i.e.

<asp:ScriptManager runat="server" EnablePageMethods="true">
</asp:ScriptManager>
2
  • thank you ... why wasn't I able to find this .d help anywhere else online?
    – Kevin
    Commented May 20, 2015 at 14:12
  • @Kevin No problem, it used to be a lot easier to find online elsewhere too. I only know from experiencing it myself. I think it was for json hijacking security issues.
    – hutchonoid
    Commented May 20, 2015 at 14:16
0

I think you should use $.ajax instead of $.post in your js method, as the GET http method is the default used in controllers if not specified otherwise.

0

I think you are close. In my project I am using $.ajax. I have provided the code sample from my project (which works fine) and I hope it helps.

C#

    [WebMethod]
    public static List<ConnectionStatusInfo> GetConnectionStatus()
    {
       .....
    }

JavaScript

$.ajax({    url: "ConnectionStatus.aspx/GetConnectionStatus",
            dataType: "json",
            type: 'POST',
            data: null,
            contentType: "application/json; charset=utf-8",
            success: function (data) {

            },
            error: function (d) {

            }
        });

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.