3

クライアント スクリプトに 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"} 

誰でも私を助けてください。前もって感謝します。

4

1 に答える 1

4

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 
}

GET の代わりに POST を使用しないでください。

「POSTリクエストは、ブックマークしたり、電子メールで送信したり、再利用したりすることはできません。ブラウザの戻る/進むボタンを使用して適切なナビゲーションを台無しにします.1回の一意の操作でサーバーにデータを送信するためにのみ使用し、(通常)サーバーはリダイレクトで応答します。」- deceze ( GET の代わりに POST を使用すると、何か良くないことはありますか? )

于 2015-10-05T11:30:58.390 に答える