jqGrid - 删除操作错误 - “错误状态:'方法不允许'。错误代码:405”

Posted

技术标签:

【中文标题】jqGrid - 删除操作错误 - “错误状态:\'方法不允许\'。错误代码:405”【英文标题】:jqGrid - Delete operation Error- "error Status: 'Method Not Allowed'. Error code: 405"jqGrid - 删除操作错误 - “错误状态:'方法不允许'。错误代码:405” 【发布时间】:2015-12-16 19:34:24 【问题描述】:

我正在使用 MVC4 WebApi 创建一个 jqGrid。我能够将数据填充到网格中,但在单击删除按钮时出现错误。这是我的前端(htm)代码。 :

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>My First Grid</title>
    <link href="../Content/Site.css" rel="stylesheet"/>
    <!--<link href="../Content/themes/base/jquery.ui.all.css" rel="stylesheet"/>-->
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/redmond/jquery-ui.css"/>
    <!--<link href="../Content/ui.jqgrid.css" rel="stylesheet"/>-->
    <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/free-jqgrid/4.9.2/css/ui.jqgrid.css"/>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css"/>
    <style>
        html, body 
            font-size: 75%;
        
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <!--<script src="../Scripts/jquery-1.9.1.min.js"></script>-->
    <!--<script src="../Scripts/jquery-ui-1.10.4.js"></script>-->
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    <script src="../Scripts/free-jqGrid/jquery.jqgrid.src.js"></script>

    <script>
        //<![CDATA[
        $(document).ready(function () 
            "use strict";
            var apiUrl = "/WebApiOne/api/task/";
            jQuery("#gridMain").jqGrid(
                url: apiUrl,
                editurl: apiUrl,
                datatype: "json",
                gridview: true,
                height: "auto",
                iconSet: "fontAwesome",
                autoencode: true,
                sortable: true,
                viewrecords: true,
                loadonce: true,
                jsonReader:  id: "TaskID" ,
                prmNames:  id: "TaskID" ,
                colNames: ["TaskID", "ProjectID", "ProjectName", "TaskName", "TaskStatus"],
                colModel: [
                     name: "TaskID", width: 60, key: true, editable: false, sorttype: "int" ,
                     name: "ProjectID", width: 90 ,
                     name: "ProjectName", width: 190 ,
                     name: "TaskName", width: 170, align: "right" ,
                     name: "TaskStatus", width: 170, align: "right" 
                ],
                cmTemplate:  editable: true ,
                //autowidth: true,
                formEditing: 
                    width: 400,
                    reloadGridOptions:  fromServer: true ,
                    serializeEditData: function (postdata) 
                        var copyOfPostdata = $.extend(, postdata);
                        if (postdata.TaskID === "_empty")  // ADD operation
                            postdata.TaskID = 0; // to be easy to deserialize
                        
                        delete copyOfPostdata.oper; // remove unneeded oper parameter
                        return copyOfPostdata;
                    
                ,
                formDeleting: 
                    mtype: "DELETE",
                    reloadGridOptions:  fromServer: true ,

                    serializeDelData: function () 
                        return ""; // don't send any body for the HTTP DELETE
                    ,
                    onclickSubmit: function (options, postdata) 
                        var p = $(this).jqGrid("getGridParam"); // get reference to internal parameters
                        p.datatype = "json";
                        options.url = apiUrl + encodeURIComponent(postdata[0]);
                    
                ,
                pager: true
            ).jqGrid("navGrid", addtext:"add" , deltext:"del", edittext:"edit", 
                mtype: "PUT",
                onclickSubmit: function (options, postdata) 
                    //var p = $(this).jqGrid("getGridParam"); // get reference to internal parameters
                    //p.datatype = "json"; // reset datatype to reload from the server
                    options.url = apiUrl + encodeURIComponent(postdata[this.id + "_id"]);
                
            ).jqGrid("filterToolbar") // add searching toolbar for local sorting (bacsue of loadonce:true) in the grid
                .jqGrid("gridResize");
        );
        //]]>
    </script>
</head>
<body>
    <table id="gridMain"></table>
</body>
</html>

我对此进行了很多研究,做出了很多变化,但结果始终相同:(。尝试了此处提供的solution,但仍然没有成功。我在GetDelete 上保留了Delete 我的方法服务器端代码。只有 Get 方法在页面加载时命中断点。Delete 方法断点永远不会命中。这是我的服务器端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;

namespace WebApiOne.Controllers

    public class Task
    
        public int TaskID  get; set; 
        public int ProjectID  get; set; 
        public string ProjectName  get; set; 
        public string TaskName  get; set; 
        public string TaskStatus  get; set; 
    

    public class TaskController : ApiController
    
        // GET api/task
        public IEnumerable<Task> Get()
        
            Task[] tasks = new Task[2];

            tasks[0] = new Task()
            
                TaskID = 1,
                ProjectID = 1,
                ProjectName = "ProjectOne",
                TaskName = "FirstPage Development",
                TaskStatus = "InProgress"

            ;

            tasks[1] = new Task()
            
                TaskID = 2,
                ProjectID = 1,
                ProjectName = "ProjectOne",
                TaskName = "Second Page Development",
                TaskStatus = "Yet To Start"

            ;

            return tasks;
        

        // DELETE api/task/5
        public void Delete(int id)
        
            // Delete row in DB.
           

    

这里是删除请求的提琴手踪迹:

DELETE http://localhost/WebApiOne/api/task/2 HTTP/1.1
Host: localhost
Connection: keep-alive
Accept: */*
Origin: http://localhost
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.93 Safari/537.36
Referer: http://localhost/WebApiOne/Views/JqGrid.htm
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-US,en;q=0.8

Here 是错误的屏幕截图:

欢迎所有建议:)。

【问题讨论】:

【参考方案1】:

我无法在我的计算机上重现该问题。无论如何,我确信您可以解决服务器端配置问题。例如the article 描述了关闭问题。可能您与 IIS 上安装的 WebDAV 有冲突。文章建议在&lt;system.webServer&gt;&lt;handlers&gt; 中添加包含&lt;remove name="WebDAV" /&gt; 的行。例如,您可以修改web.config 的以下&lt;system.webServer&gt; 部分

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

到下面

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
        <remove name="WebDAV" /> <!-- The Modification !!! -->
        <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
        <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
</system.webServer>

另外你可以换行

<modules runAllManagedModulesForAllRequests="true" />

&lt;system.webServer&gt; 部分web.config

<modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule"/> <!-- add this -->
</modules>

见the answer。

更新:如果以上都不能帮助你,那么我建议你卸载 WebDAV 模块来验证我们问题的原因是否真的是 WebDAV。如果您不需要 WebDAV,我建议您卸载模块。如果您可能需要 WebDAV,那么我建议您在 https://serverfault.com/ 上发布问题。如果卸载 WebDAV 解决了问题,那么您可以确定您只有 IIS 的相应配置和使用网站工作不需要的 WebDAV 模块的问题。

【讨论】:

我已经从 IIS7 中删除了网站并安装在 IIS Express 上,问题得到了解决。为什么会有这样的行为?我已经尝试了答案中提到的更改,但这些也不适用于 IIS7。 IIS Express 适用于默认 web.config @SandeepKushwah:回答你的问题是不同的,因为这都是 IIS 的配置问题。您是否在 IIS 上安装了 WebDAV?你需要它吗?您是否在网站上使用启用的“Windows 身份验证”?您可以在站点的 IIS 配置中禁用 WebDAV(单击“禁用 WebDAV”,请参阅 here)。您使用哪种操作系统? @SandeepKushwah:the thread 上的最后一篇文章中描述了另一种方法。如果以上都无济于事,那么您可以通过临时卸载​​ WebDAV 来验证您是否真的遇到了 WebDAV 问题。您至少可以暂时卸载该功能(请参阅here)以验证它是您问题的根源吗? 我的操作系统是 - Windows 7 Ultimate。我正在尝试您在 cmets 中建议的所有更改。完成后我会更新你。 卸载 WebDAV 解决了 IIS7 的问题。因此,请再发布一个指定卸载 WebDAV 的答案,以便我可以将其标记为问题的答案。

以上是关于jqGrid - 删除操作错误 - “错误状态:'方法不允许'。错误代码:405”的主要内容,如果未能解决你的问题,请参考以下文章

jqGrid:字体真棒图标

使用本地网格删除Free-jqGrid中的多行(4.15)

jqgrid中的确认框

如何在jqGrid中禁用所选列的搜索选项?

jqgrid 主键列的设定

jqGrid删除多行数据问题