-1

I have a webforms application and need to make an jquery ajax call to a PageMethod (i.e. WebMethod) in code behind from my aspx page. So far it does not work for me. Is this possible? Here is my code:

    $(function()
    {
        setInterval(function(){
            $.ajax({
                type: "GET",
                url: "/ClientBillingBillDetails.aspx/MyPageMethod",
                data: {someData: '<%= TheData %>'},
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function(result) {

                }
            });
        }, 10000);
    });

    [System.Web.Services.WebMethod]
    public static string MyPageMethod(int someData)
    {
        return "";
    }

Is something wrong with my URL or something else?

Thanks

David Shochet
  • 5,035
  • 11
  • 57
  • 105
  • There are a number of articles out there. Check [this](http://stackoverflow.com/questions/7770679/jquery-ajax-call-to-an-asp-net-webmethod), [this](http://www.aspsnippets.com/Articles/Calling-ASPNet-WebMethod-using-jQuery-AJAX.aspx) and [this](http://stackoverflow.com/questions/6928533/calling-a-webmethod-with-jquery-in-asp-net-webforms) – techspider Jul 26 '16 at 16:56
  • What does "does not work" mean? Describe exactly the behavior you're seeing and what you expect to see. Include relevant information from debugging and examining the console and network tabs in your browser. – mason Jul 26 '16 at 17:03
  • Sorry, "does not work" means the method is not called. – David Shochet Jul 26 '16 at 17:06
  • Check [this answer about how to make and call a Page method](http://stackoverflow.com/questions/30082988/function-not-defined-error-while-using-asp-net-ajax/30083658#30083658), or for more detail check the topic in [Stack Overflow Documentation](http://stackoverflow.com/documentation/asp.net/1411/page-methods) – Enrique Zavaleta Jul 26 '16 at 17:06
  • 1
    You still have not included enough information in your question. What method is not called? Have you checked the steps leading up to that message? Watched network traffic to see if an HTTP request is sent? Is your JavaScript AJAX call even reached? What happens if you add an error handler to your AJAX call? – mason Jul 26 '16 at 17:09
  • The javascript ajax call is reached, and the error returned is "The resource cannot be found". – David Shochet Jul 26 '16 at 17:21
  • In your browser's network tools, is the request going to the correct URL? You're using a site relative path instead of application root relative. – mason Jul 26 '16 at 17:27

2 Answers2

0

Use type as post and make sure if you have ajax.jquery library reference added in solution.

Also i think you can remove '/' in specifying method.. Just use "ClientBillingBillDetails.aspx/MyPageMethod".

Else you can use simple pageMethods using scriptmanager

0

Try This:

$(function () {
            setInterval(function () {
                $.ajax({
                    type: "POST",
                    url: "/ClientBillingBillDetails.aspx/MyPageMethod",
                    data: "{ 'someData': '<%= TheData %>' }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (result) {

                }
            });
        }, 10000);
        });
Sanjay Radadiya
  • 1,254
  • 15
  • 22