I download a source code, and I tried it in Visual Studio 2013, and it didn't work, but it works when I use Visual Studio 2010, which I think there is a trick in ASP.Net 4.5 that I don't know about. Here is the code:
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();
}
})
};
And this is the Webmethod that never run:
[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();
}
Any suggestion?
Thanks.
Have you tried stepping through your javascript? I bet you're getting a 500 error.
UPDATE
OP is getting 401 unauthorized when the AJAX request tries to call the
WebMethod
.Once you allow authentication, you have to set
AutoRedirectMode = RedirectMode.Off
in yourAppStart/RouteConfig.cs
.See more here