从JQuery调用Web API方法时,不允许出现HTTP 405方法错误

时间:2022-10-29 19:37:58

I have the following method in my Web API controller

我的Web API控制器中有以下方法

[HttpGet]
    [ActionName("GetByModule")]
    public Object Get([FromUri]int id)
    {
        //var dblayer = new Db(WebConfigurationManager.ConnectionStrings["ConnectionString"]);

        var annDb = new ContactsDB(WebConfigurationManager.ConnectionStrings["ConnectionString"]);

        return  annDb.GetContacts(id).Tables[0];
    }

Here i the Jquery code which i am using to call the method

这里是我用来调用方法的Jquery代码

$.ajax({
        type: "GET",
        contentType: "application/json",
        url: link,
        data: null,
        dataType: "json",
        success: function (data) {
            alert(data.d);

        },
        error: function (jqXHR, textStatus, err) {

            alert("Error");

        }
    });

The URL which is getting called is

被调用的URL是

http://localhost:56834/api/Contacts/GetByModule?id=9

But i keep getting HTTP 405 Method Not Allowed on calling it from Jquery.

但是我从Jquery调用它时不断获得HTTP 405方法。

Any idea what i may be doing wrong.

知道我可能做错了什么。

Thanks in Advance

提前致谢

1 个解决方案

#1


1  

Can you make sure you are making a "GET" request? (maybe from Fiddler or browser's debug mode). I say this because you seem to setting the "contentType" property in your jquery, which ideally should not be present as you should not be sending body in a "GET" request. Could you share your full raw request(may be from Fiddler)?

你能确定你正在提出“GET”请求吗? (可能来自Fiddler或浏览器的调试模式)。我这样说是因为你似乎在你的jquery中设置了“contentType”属性,理想情况下不应该存在,因为你不应该在“GET”请求中发送正文。你可以分享你的完整原始请求(可能来自Fiddler)吗?

#1


1  

Can you make sure you are making a "GET" request? (maybe from Fiddler or browser's debug mode). I say this because you seem to setting the "contentType" property in your jquery, which ideally should not be present as you should not be sending body in a "GET" request. Could you share your full raw request(may be from Fiddler)?

你能确定你正在提出“GET”请求吗? (可能来自Fiddler或浏览器的调试模式)。我这样说是因为你似乎在你的jquery中设置了“contentType”属性,理想情况下不应该存在,因为你不应该在“GET”请求中发送正文。你可以分享你的完整原始请求(可能来自Fiddler)吗?