在对Web API的AJAX POST调用中不允许获取405错误方法

在对Web API的AJAX POST调用中不允许获取405错误方法,ajax,api,post,cors,Ajax,Api,Post,Cors,我试图从AJAX POST调用中调用我的Web API函数,但始终得到一个405错误。我一直在寻找,但只是在寻找解决方案,说要启用CORS,我已经这样做了,但没有任何效果。现在,我只是尝试用html显示数据。用户输入订单号,并填写Web API函数的参数。谁能告诉我出了什么问题吗 MyWeb API函数: <Route("{ordNbr}/contents")> <HttpGet> Public Function GetOrderContents(ByVal ordNbr

我试图从AJAX POST调用中调用我的Web API函数,但始终得到一个405错误。我一直在寻找,但只是在寻找解决方案,说要启用CORS,我已经这样做了,但没有任何效果。现在,我只是尝试用html显示数据。用户输入订单号,并填写Web API函数的参数。谁能告诉我出了什么问题吗

MyWeb API函数

<Route("{ordNbr}/contents")>
<HttpGet>
Public Function GetOrderContents(ByVal ordNbr As String) As SPFolderOver

    Dim retValue As ReturnVal
    retValue = GetOrderInformation(ordNbr)
    Dim sp = New Sharepoint()
    Dim t = sp.GetOrderContent(retValue.Year, retValue.SONumber)
    Dim contentFolder = New SPFolderOver()
    contentFolder = SPFolderOver.LoadFolder(t)
    contentFolder.FolderName = t.FullPath
    Return contentFolder
End Function
function GetOrder() {


        var order = document.getElementById("orderNbr").value;

        $.ajax({
            cache: false,
            type: "POST",
            url: "http://localhost:54754/orders/{ordNbr}/contents",
            data: { 'ordNbr': order },
            success: function (data) {
                $("#orderContents").html(data);
            }
        });

    };
在myglobal.asax中

Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*")
    If (HttpContext.Current.Request.HttpMethod = "OPTIONS") Then
        HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache")
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE")
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept")
        HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000")
        HttpContext.Current.Response.End()
    End If

End Sub
在myWeb.config中

<httpProtocol>
  <customHeaders>
    <remove name="Access-Control-Allow-Origin" />
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, HEAD, OPTIONS" />
    <add name="Access-Control-Allow-Headers" value="Origin, X-Olaround-Debug-Mode, Authorization, Accept" />
    <remove name="Access-Control-Allow-Headers" />
    <add name="Access-Control-Expose-Headers" value="X-Olaround-Debug-Mode, X-Olaround-Request-Start-Timestamp, X-Olaround-Request-End-Timestamp, X-Olaround-Request-Time, X-Olaround-Request-Method, X-Olaround-Request-Result, X-Olaround-Request-Endpoint" />
  </customHeaders>
</httpProtocol>

很高兴您能够使用
网络
选项卡进行调试。注意,我建议将JSON响应命名为
response
,以便与输入的
数据
区分开来

function GetOrder() {
    var order = document.getElementById("orderNbr").value;
    $.ajax({
        type: "GET",
        data: { 'ordNbr': order },
        url: "http://localhost:54754/orders/{ordNbr}/contents)",
        cache: false,
        success: function (response) {
            $("#orderContents").html(response);
        },
        error: function (err) {
            alert("Error: " + err.errorText);
        }
   });
}

在Chrome的
网络
选项卡中,调用GetOrder()时会看到什么?Ajax调用的格式正确吗?您是否看到
ordNbr
的正确值?您从服务器返回了什么响应/数据?如果您可以将错误的屏幕截图添加到您的帖子中,将会有所帮助。在
网络
选项卡中,我将返回
{“消息”:“请求的资源不支持http方法'post'。”
以及
%7BordNbr%7D
。我将我的AJAX调用从
POST
更改为
GET
,当我查看浏览器页面时,这似乎可以将数据拉过来。现在我只需要得到要显示的数据。