【问题标题】:$ajax done function is not working in ASP.NET -MVC5 application$ajax done 函数在 ASP.NET -MVC5 应用程序中不起作用
【发布时间】:2015-10-13 10:09:21
【问题描述】:

我在局部剃须刀视图上使用 $ajax jquery 函数来获取另一个局部视图以及从控制器到页面的强类型模型数据--> 显示在特定的 div 中。现在,如果数据模型数据在那里它可以工作,但如果没有模型数据,我将传递 json 响应,以便我可以检查剃刀视图以避免空异常。我的问题是 $ajax 中的 done 方法没有调用加 json 响应,我不知道我在哪里做错了

Ajax 函数

$(document).ready(function () {

    /*Address*/
    $.ajax({
        url: '@Url.Action("DisplayStudentAddress")',
        type: "GET",
        cache: false
    }).done(function (data, textStatus, jqXHR) {

        alert(data.Response);

       $('#studentAddressDisplay').html(data);

    }).fail(function (jqXHR, textStatus, errorThrown) {

        alert(jqXHR +"    "+textStatus+"    "+errorThrown);
    });
});

ActionResult 方法

 [HttpGet]
 [Authorize]
 public ActionResult DisplayStudentAddress()
    {
        int _studentEntityID = 0;

        _studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());

        Address _studentAddressModel = new Address();

        _studentAddressModel = _studentProfileServices.GetStudentAddressByStudentID(_studentEntityID);


        if (_studentAddressModel != null)
        {
            return PartialView("DisplayStudentAddress_Partial", _studentAddressModel);
        }
        else
        {
             return Json(new { Response = "Provide Your Address Detail!" });
        }
    }
    #endregion

我已检查调试,在控制器中调用了 json,但它在 ajax 中警告错误

【问题讨论】:

  • $('#studentAddressDisplay').html(result); 必须是 $('#studentAddressDisplay').html(data);response 不存在)是错字吗?
  • 你的控制台有错误吗?
  • 对不起我的错,但还是一样
  • 不工作....错误函数被调用
  • 我需要告诉ajax期待json吗???

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


【解决方案1】:

如果您的服务器为 json 响应返回空字符串,jQuery 会将其视为失败。并且空字符串被认为是无效的 json。

根据官方文档here.

从 1.9 开始,为 JSON 数据返回的空字符串被认为是 格式错误的 JSON(因为它是);现在这将引发错误。

尝试下面的代码,而不是使用.always:-

$.ajax({
        url: '@Url.Action("DisplayStudentAddress")',
        type: "GET",
        cache: false
    }).always(function (data, textStatus, jqXHR) {

    alert(data.Response);

   $('#studentAddressDisplay').html(data);

}).fail(function (jqXHR, textStatus, errorThrown) {

        alert(jqXHR +"    "+textStatus+"    "+errorThrown);
    });

因为 .done 仅在一切正常运行时才会执行,所以如果出现问题,.done 将不会被调用。但是无论您的 ajax 请求是否有效,always 方法总是会被触发。

【讨论】:

  • 是吗?你能提供一些证据吗? (顺便说一句,我并不是说我不相信你——但返回 null 是有效的)
  • 从 1.9 开始,为 JSON 数据返回的空字符串被认为是格式错误的 JSON(因为它是);这现在将引发错误。 jquery.com/upgrade-guide/1.9/…@jumpingcode
  • 哦,有趣,我不知道。来自我的 +1
【解决方案2】:

正确答案在控制器动作方法中,修改如下;

 return Json(new { Response = "Provide Your Address Detail!", RecordStatus ="Not Available" }, JsonRequestBehavior.AllowGet);

【讨论】:

    【解决方案3】:

    为此更改代码:

     if (data.itemCount == 0) {
                            $('#row-' + data.deleteId).fadeOut('slow');
                        } else {
                            $('#item-count-' + data.deleteId).text(data.itemCount);
                        }
    
                        $('#cart-total').text(data.cartTotal);
                        $('#update-message').text(data.message);
                        $('#cart-status').text(data.cartCount);
    

    【讨论】:

      猜你喜欢
      • 2019-06-25
      • 1970-01-01
      • 2019-10-21
      • 1970-01-01
      • 2016-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      相关资源
      最近更新 更多