【问题标题】:jQUery ajax call to asp.net webforms returns html page instead of calling the specified method in the urljQUery ajax调用asp.net webforms返回html页面而不是调用url中指定的方法
【发布时间】:2013-10-16 08:38:30
【问题描述】:

您好,我对 asp.net webforms 的经验很少,但我有一种情况,每次启动应用程序或更改页面时,我都必须在服务器上执行 ajax 调用。

考虑到这一点,我在 MasterPage.Master 文件中添加了这个方法:

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

并为其添加了一个刹车点,以便我可以看到它何时被调用。

这是我正在创建的 ajax 调用:

$(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);
    }
});

})

问题是这个调用返回的是html页面的内容,而不是调用我需要的方法。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: c# javascript jquery asp.net webforms


    【解决方案1】:

    我想你错过了从你的网络方法返回值到 json

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

    Call Webmethod with json in asp.net

    【讨论】:

      【解决方案2】:

      我建议您将 ajax 方法写入除 masterPage 之外的其他 aspx 页面,并从该页面中删除 html 内容。仅将该页面用于编写通过 ajax 调用的 Web 方法。 所以你的 ajax web 方法页面必须只有页面指令,没有别的。

      这是另一种调用web方法的方式,完全是asp.net方式,不需要使用jquery

      Calling C# function through Javascript (without Json)

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-30
        • 2011-08-03
        • 2014-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多