3

Hi I have very little experience with asp.net webforms but I have a situation where I have to execute an ajax call on the server every time the application is started or the page is changed.

Taking that into consideration I have added this method in the MasterPage.Master file:

 [WebMethod]
 public static void DeleteUnpostedDocumentsFromFileShare()
 {
     var ceva = "I was called";
 }

And added a brakepont to it so I can see when it is called.

This is the ajax call I am creating:

$(document).ready(function() {
$.ajax({
    type: "POST",
    url: "/Masterpage.Master/DeleteUnpostedDocumentsFromFileShare",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        alert(data);
    },
    error : function(data , data2 , data3) {
        alert(data);
    }
});

})

The problem is that this call returns the content of the html page instead of calling the method I needed.

Can anyone tell me what I am doing wrong?

Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
aleczandru
  • 5,319
  • 15
  • 62
  • 112

2 Answers2

5

I think u missed to return value to json from ur webmethod

    [WebMethod]
    public static string DeleteUnpostedDocumentsFromFileShare()
    {
        var ceva = "I was called";
        return ceva;
    }

Call Webmethod with json in asp.net

Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
1

I would suggest you write your ajax method to some other aspx page other than masterPage and remove the html content from that page.Use that page solely for writing web methods to be called via ajax. So your ajax web method page must have only page directives and nothing else.

And here is another way to call web method totally asp.net way, no need for using jquery

Calling C# function through Javascript (without Json)

Hope this helps

Community
  • 1
  • 1
iJade
  • 23,144
  • 56
  • 154
  • 243