问题
I'm hoping someone can shed some light on why I may be receiving a 405 errors on my ASP.Net VB.Net WebMethod when attemping to call from JQuery ajax method.
Server Implementation:
<WebMethod()> _
Public Shared Function DoSomething(id As String) As String
Dim vm As HssViewModel = New HssViewModel()
Dim jResult As String = JsonConvert.SerializeObject(vm)
Return jResult
End Function
Javscript Implementation:
$.ajax({
type: "POST",
url: "mypage.aspx/DoSomething",
contentType: "application/json; charset-utf-8",
data: { 'id': 'ABC12345' },
dataType: "json",
cache: true,
succes: function (data) {
context = data;
console.log(data);
},
error: function (err) {
console.log("JQUERY ERROR RESPONSE: " + err.message);
}
});
I am consistently receiving the following error message:
POST http://localhost/mypage.aspx/DoSomething 405 (Method Not Allowed)
I have also tried setting up the script method tag to allow a GET request but then I receive a 404
<ScriptMethod(UseHttpGet:=True, ResponseFormat:=ResponseFormat.Json)> _
回答1:
Your WebMethod is expecting a GET request:
<ScriptMethod(UseHttpGet:=True
but you are doing a POST request:
$.ajax({
type: "POST",
A 405 is thrown by IIS when an HTTP verb(GET,PUT,POST,DELETE,HEAD,etc.) is requested and is not supported/disallowed by the designated handler.
If after changing the discrepancy above the problem persist then look at your iis handler mappings.
回答2:
Try adding the following lines to your web.config. I had this problem after deploying to a web server but did not experience while debugging locally. This goes in the section.
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
来源:https://stackoverflow.com/questions/20101621/asp-net-webmethod-405