C# Webmethod不会启动
我下载了一个源代码,并在Visual Studio 2013中尝试了它,但它不起作用,但在我使用Visual Studio 2010时它起作用,我认为ASP.Net 4.5中有一个我不知道的技巧。代码如下:C# Webmethod不会启动,c#,asp.net,webmethod,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
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();
}
})
};
这是从未运行过的Webmethod:
[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();
}
有什么建议吗
谢谢。您是否尝试过单步执行javascript?我打赌你会有500个错误 更新 当AJAX请求试图调用WebMethod时,OP被授权 一旦允许身份验证,就必须在AppStart/RouteConfig.cs中设置AutoRedirectMode=RedirectMode.Off
查看更多信息您是否尝试从签名中删除静态内容。。公共静态字符串LoadImagesint TakeWebMethod在Skip中需要是静态的,因为它们是无状态的,不需要实例@AswarthaI我对webmethods了解不多。看看MSDN是否能帮助您。。是的,我试过了,但没有成功。请详细描述正在发生的事情和您预期会发生的事情,然后展开“没有成功”。还有哪些调试步骤,例如浏览器的开发工具显示了什么?我没有在javascript中一步一步地进行,但我发布了一些警报以便向我显示结果,所有这些步骤都正常工作,而且在VS2010中也正常工作。我更改了这两行,但没有任何更改。我在没有得到任何信息后删除了这些行,但我按照您所说的一步一步地尝试了,它给了我以下错误:data=Object{Message:Authentication failed.,StackTrace:null,ExceptionType:System.invalidooperationexception}@Amir更新了答案,请看我在RouteConfig中发布的关于编辑重定向的链接。csI看到了这个链接,但没有成功。当我将其更改为settings.AutoRedirectMode=RedirectMode.Off时;一旦出现ajax行,它就会跳过它,并永久显示进度条。好的,我发现了问题。我按照你说的@ragerory更改了数据,这导致了问题,但我需要你发布的链接,所以它起作用了。谢谢