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

时间:2013-06-17 18:54:25

标签: asp.net-mvc rest c#-4.0 web

我的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];
    }

这里是我用来调用方法的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");

        }
    });

要调用的URL是

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

但是我一直在从Jquery调用它时不允许HTTP 405方法。

知道我可能做错了什么。

先谢谢

1 个答案:

答案 0 :(得分:1)

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

相关问题