Ajax 调用 MVC 方法导致 404

Ajax call to MVC method results in 404

我正在使用 Canvasjs 和 MVC5 开发下钻图表。我有一个名为 JsonController 的控制器,其中包含多个 return Json 的任务。它们都非常相似,但随着层数的增加接受更多的论点。第 0 层是默认层。

[HttpPost]
public async Task<IActionResult> GetLayer0(string datePassedIn)
{
    string orgCode = User.Identity.GetOrgCode();
    DateTime? processDate;
    DateTime defaultDate = DateTime.Today.AddDays(-1);  //default yesterday 

    try
    {
        processDate = DateTime.ParseExact(datePassedIn, inputDateFormat, cultureProvider);
    }
    catch (FormatException ex)
    {
        _logger.LogError(ex, "Error formatting date {datePassedIn} did not match {inputDateFormat}. using default date {defaultDate}", null);
        processDate = defaultDate;
    }

    List<DataPoint> dataPoints = new List<DataPoint>();

    IEnumerable<EventTypeLayer1> results = await _context.EventTypeLayer1Results
                                                        .FromSql($"usp_dd_EventType_0 @p0, @p1", orgCode, processDate)
                                                        .ToListAsync();

    foreach (EventTypeLayer1 result in results)
    {
        dataPoints.Add(new DataPoint(result.Value, result.Title, result.Colour));
    }

    return Json(dataPoints);
}   

在javascript中,ajax调用是用数组

管理的
var ajaxOptions  = [
{

    url: "~/Views/Json/GetLayer0",
    data: {
        layer: 0,
        processDate: encodeURIComponent(formatDateInput(param.processDate)),
        orgCode: encodeURIComponent(param.orgCode)
    },
    callback : handleLayer0
},
{
    url: "~/Views/Json/GetLayer1",
    data: {
        layer: 1,
        processDate: encodeURIComponent(formatDateInput(param.processDate)),
        orgCode: encodeURIComponent(param.orgCode),
        eventType: encodeURIComponent(param.eventType)
    },
    callback : handleLayer1

},
{
    url: "~/Views/Json/GetLayer2",
    data: {
        layer: 2,
        processDate: encodeURIComponent(formatDateInput(param.processDate)),
        orgCode: encodeURIComponent(param.orgCode),
        eventType: encodeURIComponent(param.eventType),
        driverId: encodeURIComponent(param.driverId)
    },
    callback : handleLayer2

}
];

function doAjax( layerIndex) {
$.ajax({
    type: "POST",
    cache: false,
    dataType: "json",
    url: ajaxOptions[layerIndex].url,
    data: ajaxOptions[layerIndex].data,
    success: function (serverResponse) {

        //once a successful response has been received,
        //no HTTP error or timeout reached,
        //run the callback for this request
        ajaxOptions[layerIndex].callback(serverResponse);

    },
    complete : function () {

        //note that the "success" callback will fire
        //before the "complete" callback
        console.log("Ajax call complete");
    }
});
}

当 ajax 触发时,出现错误

https://localhost:44388/~/Views/Json/GetLayer0 错误 404

https://localhost:44388/Json/GetLayer0 错误 405

@Url.Action("GetLayer0", "JsonController") 呈现空白

我有点困惑。我该怎么办?

编辑:这是实际的 AJAX 调用

function doAjax( layerIndex) {
    $.ajax({
        type: "POST",
        cache: false,
        dataType: "json",
        url: ajaxOptions[layerIndex].url,
        data: ajaxOptions[layerIndex].data,
        success: function (serverResponse) {

            //once a successful response has been received,
            //no HTTP error or timeout reached,
            //run the callback for this request
            ajaxOptions[layerIndex].callback(serverResponse);

        },
        complete : function () {

            //note that the "success" callback will fire
            //before the "complete" callback
            console.log("Ajax call complete");
        }
    });
 }

您正在调用视图 url 而不是控制器函数 应该是

{

    url: "/youcontrollername/GetLayer0",
    data: {
        layer: 0,
        processDate: encodeURIComponent(formatDateInput(param.processDate)),
        orgCode: encodeURIComponent(param.orgCode)
    },
    callback : handleLayer0
},