Я все время чесал голову. Мой код работает на IIS Express в VS Studio 2017, но после развертывания на IIS Server 10 он говорит: Failed to load resource: POST http://localhost:99 jquery?v=2u0aRenDpYxArEyILB59ETSCA2cfQkSMlxb6jbMBqf81:1 /Home/ListDevices 404 (Not Found) IIS 10.0.
. Сервер не может найти URL AJAX. Я использую ADO.net для других таблиц базы данных. Я пробовал это решение asp.net mvc5 ajax post возвращает 404 после переключения на IIS 7.5 из IIS Express , а также asp.net mvc ajax post возвращает 404 не найдено . Все они не работают, чтобы решить мою проблему. Я не знаю, что не так с моим кодом. Вот мой код.
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;
}
}
У кого-нибудь есть идеи?