1

First of all, I am NOT using MVC (don't ask why)

From reading other posts on here it seems I need to use a "WebMethod" approach but I cannot seem to get my function to call properly.

The Chrome console gives me a 404 error "Cannot find /ajax/TestAjax". So I know I'm not locating the function correctly. But I can't seem to find where to put it.

Any advice is much appreciated!


File locations:

enter image description here

jQuery code:

$("#btn_adduser").click(function () {
        var isValid = validateAddUser();
        if (isValid.length > 2) {
            alert(isValid);
        }
        else {
            $.ajax({
                type: "POST",
                url: "/ajax/TestAjax",
                data: "",
                success: function (data) {
                    alert(data);
                }
            });
        }
    });

C# code: (ajax.cs)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for ajax
/// </summary>
public class ajax
{
    public ajax()
    {

    }

    [WebMethod]
    public static string TestAjax()
    {
        return "All Good";
    }

}

2 Answers 2

1

[WebMethod] is a Web Forms feature. In ASP.NET Razor Web Pages ,you can follow this link for RESTful services.

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

Comments

1

[WebMethod] is a Web Forms feature; it won't help you.

You can create an ASHX (Generic Handler) file, or use MVC / Web API.

1 Comment

If I use the Web API approach I assume I would need to add a package and then replace [WebMethod] with [HttpPost] ?

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.