2012-11-12 39 views
1

我的客戶端腳本中有一個web方法GetNextImage。在ASPX頁面中,我有以下代碼。嘗試使用GET請求調用方法 u0027GetNextImage u0027,這是不允許的

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"} 

請任何人都可以幫助我。提前致謝。

+1

嘗試鍵入:「POST」,看看你是否得到相同的錯誤 –

+0

@Kirill:非常感謝......它的工作....但現在我在setTimeout(幻燈片() ,5000);在javascript中提到...你能幫我嗎...... – JItendra

+0

它應該是'setTimeout(幻燈片,5000)'。 – Barmar

回答

1

將屬性添加到您的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?

相關問題