ASP.Net WebMethod 405

那年仲夏 提交于 2020-01-17 02:50:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!