简体   繁体   中英

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM