4

I have a web method GetNextImage in my client script. In ASPX page I have the following code.

function slideshow() {
  $.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "/RollingScreen.aspx/sample",
    dataType: "json",
    data: "{}",
    success: function (data) {
      //this changes the image on the web page
      $('#imgSlideShow').attr("src","~/Images/1.png");

      //fires another sleep/image cycle
      setTimeout(slideshow(), 5000);
    },
    error: function (result) {
      alert(result.message);
    }
  });
}

$(document).ready(function () {
  //Kicks the slideshow
  slideshow();
});

I am getting the error as below.

{"Message":"An attempt was made to call the method \u0027GetNextImage\u0027 using a GET request, which is not allowed.","StackTrace":" at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} 

Please can anyone help me. Thanks in advance.

SteD
  • 13,909
  • 12
  • 65
  • 76
JItendra
  • 65
  • 1
  • 10
  • 1
    Try type: "POST" and see if you get the same error – Kirill Ivlev Nov 12 '12 at 07:47
  • @Kirill: thanks a lot...It worked....but now i am getting error at setTimeout(slideshow(), 5000); in the javascript mentioned...Can u please help me...... – JItendra Nov 12 '12 at 07:56
  • It should be `setTimeout(slideshow, 5000)`. – Barmar Nov 12 '12 at 07:56
  • @Barmar: Thanks Barmer. Now this is also resolved.A last doubt.. The following is not setting the asp image element.....$('#imgSlideShow').attr("src", "~/Images/1.png"); What is the reason – JItendra Nov 12 '12 at 08:04
  • Does `` work in the HTML? Try `"/~/Images/1.png"`. – Barmar Nov 12 '12 at 08:06
  • Actually, I wouldn't expect either to work -- doesn't `~` need to be followed by a username, so it knows whose public_html directory to look in? – Barmar Nov 12 '12 at 08:08
  • @Barmer: yes there was error in that link...now i have given "~/1.png" in html,it displayed the image....but i am unable to set it from that script....$('#imgSlideShow').attr("imageurl","~/1.png"); is not setting the image url – JItendra Nov 12 '12 at 08:22

1 Answers1

5

Add attribute to you WebMethod to indicate using of HTTP GET.

[WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true, ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
public static string sample()
{
   //Your Code Which Gets Next Image 
}

You shouldn't use POST instead of GET.

"POST requests can't be bookmarked, send in an email or otherwise be reused. They screw up proper navigation using the browsers back/forward buttons. Only ever use them for sending data to the server in one unique operation and (usually) have the server answer with a redirect." - deceze (Is there anything not good using POST instead of GET?)

Community
  • 1
  • 1
  • Has anyone ever had an app display "An attempt was made to call the method '' using a GET request" even though you specify type: 'POST'? – Artorias2718 Jun 27 '19 at 18:30