0

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.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Amir
  • 59
  • 1
  • 2
  • 8
  • did u try by removing static from signature.. public static string LoadImages(int Skip, int Take) – Aswartha Jun 10 '15 at 12:00
  • 1
    `WebMethod` needs to be static since they are stateless and do not need an instance @Aswartha – ragerory Jun 10 '15 at 12:02
  • I don't know much about webmethods. see if MSDN can help you.. https://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx – Aswartha Jun 10 '15 at 12:04
  • Yes, I tried it, and I didn't work. – Amir Jun 10 '15 at 12:04
  • Please expand "it didn't work" with a full detailed description of what is happening and what you expect to happen. And also what debugging steps (eg. what do the browser's dev tools show?) – Richard Jun 10 '15 at 12:05
  • Have a look at this for calling WebMethod from ajax http://stackoverflow.com/questions/27917255/c-sharp-web-method-is-not-calling-in-javascript/27917333#27917333 – Mairaj Ahmad Jun 10 '15 at 12:07
  • Richard: It's an Infinite Scroll images, so when I scroll down it should load more images. I can see the progress bar gif, but the webmethod doesn't run. I'm sure it goes to success line. As I said, it works in VS 2010. – Amir Jun 10 '15 at 12:58

1 Answers1

0

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 your AppStart/RouteConfig.cs.

See more here

Community
  • 1
  • 1
ragerory
  • 1,360
  • 1
  • 8
  • 27
  • I didn't go step by step in javascript, but I put some alert in order to show me the results, and all of them were working, and again, it works in VS 2010. I changed both line, and nothing changed. – Amir Jun 10 '15 at 13:00
  • I delete the lines after I didn't get anything, but I tried it step by step as you said, and it gives me this error: data = Object {Message: "Authentication failed.", StackTrace: null, ExceptionType: "System.InvalidOperationException"} – Amir Jun 10 '15 at 13:20
  • @Amir updated the answer, please see the link I posted about editing your redirect in `RouteConfig.cs` – ragerory Jun 10 '15 at 13:26
  • I saw that one, but it didn't work out. When I change it to settings.AutoRedirectMode = RedirectMode.Off; as soon as it comes to ajax line it skips it, and shows the progress bar permanently. – Amir Jun 10 '15 at 13:34
  • OK, I found the problem. I change the data as you said @ragerory, and it caused the problem, but I needed that link that you posted, so it worked. Thanks. – Amir Jun 10 '15 at 13:42
  • @ragerory: It's ok to suggest acceptance of your answers, but please consolidate the comments, external information from the link you posted, etc., into your answer first. As it sits, somebody stumbling upon this answer would have a hard time figuring out what it means. – Cᴏʀʏ Jun 10 '15 at 13:56