Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQUery ajax call to asp.net webforms returns html page instead of calling the specified method in the url

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?

like image 637
aleczandru Avatar asked Nov 24 '25 05:11

aleczandru


2 Answers

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

like image 129
Somnath Kharat Avatar answered Nov 25 '25 19:11

Somnath Kharat


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

like image 30
iJade Avatar answered Nov 25 '25 17:11

iJade



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!