问题
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.
回答1:
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?)
来源:https://stackoverflow.com/questions/13339929/an-attempt-was-made-to-call-the-method-u0027getnextimage-u0027-using-a-get-requ