0
我在我的代碼中有一個WebMethod,我通過AJAX調用。該方法在使用GET請求時有效,但我更願意使用POST,我也想知道爲什麼這不起作用和/或我做錯了什麼。由於GET請求不允許POST請求失敗
的JavaScript
$(document).ready(function() {
$.ajax({
url: "Default.aspx/HelloWorld",
method: "POST",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
$("#test1").html(data.d);
},
error: function (err) {
$("#errOutput").text("ERROR: " + err.responseText);
}
});
});
C#
[WebMethod]
[ScriptMethod(UseHttpGet=false)]
public static string HelloWorld()
{
return "Hello World!";
}
錯誤
Message:
"An attempt was made to call the method \u0027HelloWorld\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"
哇,我不能相信我錯過了。謝謝,喬恩! – ExceptionLimeCat