我一直都在挠头。我的代码在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网址。我正在使用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 Side(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 :(得分:0)
我终于解决了我的问题。问题在于调用分离的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>   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>   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>  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>
}