我的客户端脚本中有一个Web方法GetNextImage。在ASPX页面中,我有以下代码。
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();
});
我收到如下错误。
{"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"}
任何人都可以帮助我。提前谢谢。
答案 0 :(得分:3)
向WebMethod添加属性以指示使用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
}
你不应该使用POST而不是GET。
" POST请求无法加入书签,通过电子邮件发送或以其他方式重复使用。他们使用浏览器后退/前进按钮搞砸正确的导航。只能使用它们在一个独特的操作中将数据发送到服务器,并且(通常)让服务器回答重定向。" - deceze(Is there anything not good using POST instead of GET?)