Webmethod不会触发

时间:2015-06-10 11:56:44

标签: c# asp.net webmethod

我下载了一个源代码,我在Visual Studio 2013中尝试过它并且它没有用,但是当我使用Visual Studio 2010时它会起作用,我认为在ASP.Net 4.5中有一个技巧我不知道。这是代码:

function Load(Skip, Take) {
            $('#divPostsLoader').html('<img src="../ProgressBar/ajax-loader.gif">');

            //send a query to server side to present new content
            $.ajax({
                type: "POST",
                url: "/Grid/LoadImages",
                data: "{ Skip:" + Skip + ", Take:" + Take + " }",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {                        
                    if (data != "") {
                        $('.thumb').append(data.d);
                    }
                    $('#divPostsLoader').empty();
                }
            })

        };

这是从未运行的Web方法:

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string LoadImages(int Skip, int Take)
{
    System.Threading.Thread.Sleep(2000);
    StringBuilder GetImages = new StringBuilder();
    string Imagespath = HttpContext.Current.Server.MapPath("~/Images/");
    string SitePath = HttpContext.Current.Server.MapPath("~");
    var Files = (from file in Directory.GetFiles(Imagespath) select new { image = file.Replace(SitePath, "") }).Skip(Skip).Take(Take);
    foreach (var file in Files)
    {
        var imageSrc = file.image.Replace("\\","/").Substring(1); //Remove First '/' from image path
        GetImages.AppendFormat("<a>");
        GetImages.AppendFormat("<li>");
        GetImages.AppendFormat(string.Format("<img src='{0}'/>", imageSrc));
        GetImages.AppendFormat("</li>");
        GetImages.AppendFormat("</a>");


    }
    return GetImages.ToString();
}

有什么建议吗?

感谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试过单步执行javascript?我打赌你得到500错误。

<强>更新

当AJAX请求尝试呼叫WebMethod时,OP未经授权获得401。

允许身份验证后,您必须在AutoRedirectMode = RedirectMode.Off中设置AppStart/RouteConfig.cs

查看更多here

相关问题