0

I can't seem to get this simple function working.

The AJAX call returns success but the function is never being called since the Debug.WriteLine is not showing up. The "Function has been called" alert does pop up. There are no errors in the Chrome console.

I am using ASP.NET Web Forms

The Contact.aspx.cs file:

public partial class Contact : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Debug.WriteLine("Contact Page loaded");
    }

    [System.Web.Services.WebMethod]
    public static string Test(string test)
    {
        Debug.WriteLine("Input param"+test);
        return test;
    }  
}

In the Contact.aspx file

<button type="button" class="btn btn-info" onclick="ShowTest()">Test</button>

<script type = "text/javascript">
    function ShowTest() {            

        //Tried this also (prefered)
        //var res = PageMethods.Test("testMessage");

        var testMsg = 'This is the test message';

        $.ajax({
            type: "POST",
            url: "Contact.aspx/Test",
            data: JSON.stringify({
                test: testMsg
            }),
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (result) {
                alert('It worked! ' + result.d);
            },
            error: function (result) {
                alert('Nope');
            }
        });

        alert("Function has been called");
    }       
</script>
10
  • What is in result in the success function? Out of curiosity, where are you looking for the Debug.WriteLine output? Commented Aug 9, 2017 at 18:56
  • Hi David. The result shows undefined. Commented Aug 9, 2017 at 18:57
  • The output is showing in visual studio console. I do get the "Contact Page loaded" showing up. Commented Aug 9, 2017 at 18:58
  • Is what I am doing the standard way of calling C# code from web page? Are there better ways then ajax? Commented Aug 9, 2017 at 19:04
  • No, this should be pretty standard. Though I may be missing something obvious, I haven't done WebForms in a very long time. In your browser's debugging tools, what's the server's response to the AJAX call? Commented Aug 9, 2017 at 19:05

1 Answer 1

2

I have found the solution!

Authentication failed in call webmethod from jquery AJAX

Blockquote

I found the answer

Just comment below line in RouteConfig file

//settings.AutoRedirectMode = RedirectMode.Permanent;

Sign up to request clarification or add additional context in comments.

1 Comment

I don't know what it's for but at this point I am just happy it's working. What a dreadful bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.