【发布时间】: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>   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>   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>  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