在本地一切正常(当我使用localhost运行时,但是在发布到IIS并运行后,它在浏览器控制台中引发了异常]
POSThttp://xx.xxx.x.x/Dashboard/FilterData404(未找到)
下面是我的代码,我正在使用按钮来调用此javascript函数
VIEW-索引
<script type="text/javascript">
function btnFilterClick() {
var txtFDate = $("#dpFrom");
var txtTDate = $("#dpTo");
var ddBranches = $("#ddBranches");
var ddBrands = $("#ddBrands");
$.ajax({
type: "POST",
url: "/Dashboard/FilterData",
data: '{FromDate: "' + txtFDate.val() + '", ToDate: "' + txtTDate.val() + '", SelectedBranches : "'+ ddBranches.val()+'", SelectedBrands : "'+ ddBrands.val()+'" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success:
function (r)
{
chartPurchaseVsSales.data.datasets[0].data = r.listSalesData;
chartPurchaseVsSales.data.datasets[1].data = r.listPurchaseData;
chartPurchaseVsSales.data.labels = r.xAxisNameForPurchaseAndSales;
chartPurchaseVsSales.update();
}
});
}
</script>
Controller-DashboardController
[HttpPost]
[Authorize]
public JsonResult FilterData(DashboardModel modelX)
{
DashboardModel model = new DashboardModel();
model.xAxisNameForPurchaseAndSales = new string[] { };
model.xAxisNameForStock = new string[] { };
*Skipped some codes to make it simple*
return Json(model);
}
任何主意,这是怎么了?我是网络开发的新手!救救我!
解决方案1
您可以从url: "/Dashboard/FilterData",
进行如下更改
url: '@Url.Action("FilterData", "Dashboard")'
解决方案2
HTML中
<input type="hidden" value="@Url.Action("FilterData", "Dashboard")" id='url' />
在Jquery中
url: $("#url").val()
我更改为'@Url.Action("FilterData", "Dashboard")'
,它可以正常工作!谢谢你们。
下面是我的代码
$.ajax({
type: "POST",
url: '@Url.Action("FilterData", "Dashboard")',
data: '{FromDate: "' + txtFDate.val() + '", ToDate: "' + txtTDate.val() + '", SelectedBranches : "'+ ddBranches.val()+'", SelectedBrands : "'+ ddBrands.val()+'" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success:
function (r)
{
chartPurchaseVsSales.data.datasets[0].data = r.listSalesData;
chartPurchaseVsSales.data.datasets[1].data = r.listPurchaseData;
chartPurchaseVsSales.data.labels = r.xAxisNameForPurchaseAndSales;
chartPurchaseVsSales.update();
}
});
您需要将ajax请求中的URL更改为URL:'/ YourAppName / Controller / ActionResult',发布到IIS之后。这就是为什么永远不要对URL进行硬编码的原因。您可以在剃须刀页面中使用一个隐藏字段,并填充@ Url.Action(“ FilterData”,“ Dashboard”)之类的URL,然后从那里读取它。希望对您有所帮助。