【问题标题】:JQUERY Ajax call returns IIS 10.0 Detailed Error - 404.0 - Not Found in ASP.net MVC5 using VS 2017 CommunityJQUERY Ajax 调用返回 IIS 10.0 详细错误 - 404.0 - Not Found in ASP.net MVC5 using VS 2017 Community
【发布时间】:2019-02-03 20:05:56
【问题描述】:

我一直在摸不着头脑。我的代码在 VS Studio 2017 中的 IIS Express 上运行,但在部署到 IIS Server 10 后显示:Failed to load resource: POST http://localhost:99 jquery?v=2u0aRenDpYxArEyILB59ETSCA2cfQkSMlxb6jbMBqf81:1 /Home/ListDevices 404 (Not Found) IIS 10.0.。服务器找不到 ajax url。我正在使用 ADO.net 进行其他数据库表操作。我试过这个解决方案asp.net mvc5 ajax post returns 404 after switching to IIS 7.5 from IIS Express 和这个asp.net mvc ajax post returns 404 not found。他们都没有解决我的问题。我不知道我的代码有什么问题。到目前为止,这是我的代码。

HTML 添加脚本

<script src="~/Scripts/Device-View.js"></script>
<div class="panel-body">
                <div class="row">
                    <div class="col-lg-12">

                        <table id="myDataTable" class="table table-bordered table-striped table-hover">
                            <thead style="color:black">

                            </thead>

                        </table>
                    </div>
                </div>
                <!-- /.row (nested) -->
            </div>

Jquery 端 (Device-View.js)

var datum;  
$(document).ready(function () {   
   $('#myDataTable').DataTable({
    dom: "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>" +
        "<'row'<'col-sm-12'tr>>" +
        "<'row'<'col-sm-5'i><'col-sm-7'p>>",

    buttons: [
        {
            "extend": 'print', "text": '<span class="glyphicon glyphicon-print"></span>&nbsp&nbsp Print', "className": 'btn btn-success btn-sm',
            exportOptions: {
                //columns: ':visible'
                columns: [0, 1, 2, 3, 4, 5, 6, 7]
            }
        },

        {
            "extend": 'excel', "text": '<span class="glyphicon glyphicon-print"></span>&nbsp&nbsp Excel', "className": 'btn btn-success btn-sm exportExcel',
            exportOptions: {
                //columns: ':visible'
                columns: [0, 1, 2, 3, 4, 5, 6, 7]
            }
        },
        { "extend": 'colvis', "text": '<span class="glyphicon glyphicon-list"></span>&nbsp Hide Column', "className": 'btn btn-danger btn-sm', "columnDefs": [{ "targets": -1, "visible": false }] },
    ],

    order: [[0, "desc"]],

    "columnDefs": [{
        "defaultContent": "",
        "targets": "_all"
    }],

    ajax: {
        url: "/Home/ListDevices",
        //url: '@Url.Action("ListDevices", "Home")',
        type: "POST",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (result) {

            var data = jQuery.map(result, function (key, item) {
                return [[key.DeviceCode, key.PrevDeviceCode, key.SerialNum, key.NameDescription, key.ClassDescription, key.ModelDescription, key.LocationDescription, key.StatusDescription ]];
            });

            datum = data;

            $('#myDataTable').dataTable().fnAddData(datum);
        },
        error: function (errormessage) {
            alert(errormessage.responseText);
            //alert("Error");
        }
    },


    columns: [
        { title: "Code" },
        { title: "Prev. Code" },
        { title: "Serial No." },
        { title: "Name" },
        { title: "Class" },
        { title: "Model" },
        { title: "Location" },
        { title: "Status" },
    ]

});

});

路由配置

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Walkin",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "IndexView", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        ); 
    }
}

Web.Config

<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=aspnet-sample;Integrated Security=True" providerName="System.Data.SqlClient" />

HomeController

public JsonResult ListDevices()
    {
        return Json(empDB.ListAllDevices(), JsonRequestBehavior.AllowGet);
    }

型号

//Return list of all Status Setting data
    public List<Devices> ListAllDevices()
    {
        List<Devices> lst = new List<Devices>();
        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
        {
            con.Open();
            SqlCommand com = new SqlCommand("AllDevices", con);
            com.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr = com.ExecuteReader();
            while (rdr.Read())
            {
                lst.Add(new Devices
                {
                    IDevice = Convert.ToInt32(rdr["IDDevice"]),
                    StatusDescription = rdr["Status_Desc"].ToString(),
                });
            }

            return lst;
        }
    }

有人有想法吗?

【问题讨论】:

  • 不相关,但您的两条路线都是相同的,只有第一个路线会被执行(都允许 0 到 3 段)
  • @StephenMuecke 我有管理员视图和用户视图(walkin),所以我创建了 2 条路线以删除用户视图中的按钮(添加、编辑、删除)。
  • 我认为您不理解我的评论?第二条(默​​认)路线毫无意义。删除另一个(但它不是您的错误的原因)。错误的详细信息是什么?
  • @JAMES BRYAN B. Juventud:您的 ajax 调用是 POST 方法,所以您在上面的 JsonResult ListDevices() 操作方法中提到了 [HttpPost] 吗?
  • 并且 JsonRequestBehavior.AllowGet 不是必需的,因为它是 POST

标签: jquery asp.net ajax asp.net-mvc iis


【解决方案1】:

我终于解决了我的问题。问题在于调用分离的 Javascript 文件。也许我在调用分离的 JS 文件之前错过了一些配置,并且像一个魅力一样工作。我在视图中附加了 JS 内容。请将 url ajax 更改为此 url: '@Url.Action("function", "Controller")', 希望这对其他人有帮助。

`Your HTML code here....`

@section Scripts{
<!-- JS -->
<script src="~/Scripts/jquery.dataTables.min.js"></script>
<script src="~/Scripts/dataTables.bootstrap.min.js"></script>

<script>
    var datum;
    $(document).ready(function () {
        $('#myDataTable').DataTable({

            dom: "<'row'<'col-sm-3'l><'col-sm-6 text-center'B><'col-sm-3'f>>" +
                "<'row'<'col-sm-12'tr>>" +
                "<'row'<'col-sm-5'i><'col-sm-7'p>>",

            buttons: [
                {
                    "extend": 'print', "text": '<span class="glyphicon glyphicon-print"></span>&nbsp&nbsp Print', "className": 'btn btn-success btn-sm',
                    exportOptions: {
                        columns: [0, 1, 2, 3, 4, 5, 6, 7]
                    }
                },

                {
                    "extend": 'excel', "text": '<span class="glyphicon glyphicon-print"></span>&nbsp&nbsp Excel', "className": 'btn btn-success btn-sm exportExcel',
                    exportOptions: {
                        columns: [0, 1, 2, 3, 4, 5, 6, 7]
                    }
                },
                { "extend": 'colvis', "text": '<span class="glyphicon glyphicon-list"></span>&nbsp Hide Column', "className": 'btn btn-danger btn-sm', "columnDefs": [{ "targets": -1, "visible": false }] },
            ],

            order: [[0, "desc"]],

            "columnDefs": [{
                "defaultContent": "",
                "targets": "_all"
            }],

            ajax: {
                url: '@Url.Action("ListDevices", "Home")',

                type: "POST",
                contentType: "application/json",
                dataType: "json",
                success: function (result) {

                    var data = jQuery.map(result, function (key, item) {
                        return [[key.DeviceCode, key.PrevDeviceCode, key.SerialNum, key.NameDescription, key.ClassDescription, key.ModelDescription, key.LocationDescription, key.StatusDescription ]];
                    });

                    datum = data;

                    $('#myDataTable').dataTable().fnAddData(datum);
                },
                error: function (errormessage) {
                    alert(errormessage.responseText);
                }
            },

            columns: [
                { title: "Code" },
                { title: "Prev. Code" },
                { title: "Serial No." },
                { title: "Name" },
                { title: "Class" },
                { title: "Model" },
                { title: "Location" },
                { title: "Status" },
            ]

        });

    });

</script>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2012-08-10
    • 2016-01-27
    • 2016-07-10
    • 2018-06-23
    相关资源
    最近更新 更多