【问题标题】:Jquery Ajax throw error before ActionResult return在 ActionResult 返回之前 Jquery Ajax 抛出错误
【发布时间】:2012-10-16 17:43:23
【问题描述】:

我正在解决一个奇怪的问题,这可能是由于我对 ajax 缺乏经验。出于某种原因,我的 Ajax 函数甚至在我的 ActionResult 返回 PartialView 之前就触发了我的错误警报对话框。

谁能找出我的 ajax 函数抛出错误的原因,甚至我做错了什么?

我的 Jquery

function GetSalesLinesByArea(e) {
var areaId = treeView().getItemValue(e.item);
$('#HiddenAreaId').val(areaId);
var url = '/SalesLine/IndexPartial/';
$.ajax({
    type: "GET",
    dataType: "html",
    url: url,
    data: { areaNodeId: areaId },
    success: function (data) {
        $('#HuntingGrid').html(data);
        alert("success!");
    },
    error: function () {
        alert("error!");
    }
  });
}

我的观点Index.cshtml

@using Telerik.Web.Mvc.UI
@using Veidivefur.Model.Entity
@model Veidivefur.ViewModels.SalesLineViewModel
@{
    ViewBag.Title = "Hlunnindi";
}

<div id="HuntingGrid">
    @{ Html.RenderPartial("IndexPartial"); }
</div>

我的局部视图 IndexPartial.cshtml

@using Telerik.Web.Mvc.UI
@model Veidivefur.ViewModels.SalesLineViewModel

<div>
    @(Html.Telerik().Grid(Model.SalesLines)
        .Name("Grid")
        //More Telerik stuff
</div>

*控制器中的我的索引 ActionResult *

    public ActionResult Index()
    {
        List<CombinedSalesLine> combinedSalesLines =
            GenerateSalesLines(_salesLineModel.FindAllSalesLinesLargerThanToday(0));

        return View(new SalesLineViewModel(combinedSalesLines));
    }

*控制器中的我的 IndexPartial ActionResult *

    public ActionResult IndexPartial(string areaNodeId)
    {
        int areaNodeInt = Convert.ToInt32(areaNodeId);

        List<SalesLine> saleslines = areaNodeInt == 0 ? _salesLineModel.FindAllSalesLinesLargerThanToday(0)
            : _salesLineModel.FindAllSalesLinesLargerThanToday(areaNodeInt);

        List<CombinedSalesLine> combinedSalesLines = GenerateSalesLines(saleslines);

        return PartialView("IndexPartial", new SalesLineViewModel(combinedSalesLines));
    }

【问题讨论】:

  • 你确定网址应该是/SalesLine/IndexPartial/吗?用斜线?并且没有 .html .php 或 .asp 页面?
  • 你好@RASG,是的,我很确定这是怎么做的,至少我以前在其他项目中使用过这种方法。问题是一旦 url 触发了我的 ActionResult 函数,我的 ajax 函数甚至在我的 ActionResult 返回任何内容之前就会引发错误......!?
  • 它是完全命中服务器端代码,还是只是返回错误?
  • @JeffreyLott,它实际上是在访问服务器端代码。就在我的服务器端代码将返回(在我的 IndexPartial 中)之前,ajax 会引发错误并弹出警报对话框。奇怪的是,一旦我关闭对话框窗口,服务器端代码就会继续运行,实际上会将正确的模型返回给视图,但由于某种原因,视图不会呈现新数据。

标签: jquery ajax asp.net-mvc telerik asp.net-mvc-4


【解决方案1】:

我解决了这个问题!我发布的问题代码似乎是正确的。问题在于我调用 ajax 函数的方式,我使用的 url 也调用了另一个函数,因此 ajax 函数引发了错误。我觉得很傻,但很高兴它得到了解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2018-12-02
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多