【问题标题】:jQuery ajax request results in a Not Found ErrorjQuery ajax 请求导致 Not Found 错误
【发布时间】:2015-09-05 16:41:41
【问题描述】:

我是 MVC 的新手,正在尝试一些东西,但我无法让它工作。

我有这个脚本应该根据下拉列表选择在页面内插入部分视图。

$(function () {
    $('#ddTipologiaFattura').change(function () {
        var selectedID = $(this).val();
        $.ajax({
           url: '/Admin/Fatturazione/GetPartial/' + selectedID,
           contentType: 'application/html; charset=utf-8',
           type: 'GET',
           dataType: 'html'
           })
           .success(function (result) {
               $('#partialPlaceHolder').html(result);
           })
           .error(function (xhr, status, error) {
               alert(status + '\n' + error)
           });
        });
   });

这是我的控制器~/Areas/Admin/Controllers/FatturazioneController.cs

    [RouteArea("Admin")]
    [Route("Fatturazione/{action}")]
    public class FatturazioneController : Controller
    {
        private MyEntity db = new MyEntity();

        public ActionResult GetPartial(int partialViewId)
        {
            if (partialViewId == 0)
            {
                var fatturaAziendaVM = new FatturaPerAziendaViewModel();
                ViewBag.Intestatario = new SelectList(db.Azienda, "AziendaNome", "AziendaNome");
                return PartialView("~/Areas/Admin/Views/Fatturazione/_ListaAziende.cshtml", fatturaAziendaVM);
            }
            var fatturaVM = new FatturaViewModel();
            return PartialView("~/Areas/Admin/Views/Fatturazione/_Intestatario.cshtml", fatturaVM);
        }

脚本不断收到 Not Found 错误。 我做错了什么?

【问题讨论】:

  • 您可以直接从浏览器访问您为 AJAX 请求设置的 URL 吗?
  • 不,我只能从控制器访问返回视图而不是 PartialView 的其他 ActionResults
  • 我的意思是您应该尝试直接从浏览器发出请求,以检查该 URL 是否确实可以访问。这将是调试问题的第一步。

标签: c# jquery .net asp.net-mvc


【解决方案1】:

您的路线仅考虑操作,而不考虑 Id,这就是它失败的原因。您应该更新每个操作的路由以考虑 Id,或者将 id 作为查询字符串参数附加。

 $.ajax({
       url: '/Admin/Fatturazione/GetPartial?partialViewId=' + selectedID,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-14
    • 2012-08-10
    • 2014-03-20
    • 2019-12-10
    • 1970-01-01
    • 2019-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多