简体   繁体   English

$ ajax done函数在ASP.NET -MVC5应用程序中不起作用

[英]$ajax done function is not working in ASP.NET -MVC5 application

I am using $ajax jquery function on partial razor view to get another partial view along with strongly typed model data from controller to page--> display in specific div. 我在部分剃刀视图上使用$ ajax jquery函数来获取另一个局部视图以及从控制器到页面的强类型模型数据 - >在特定div中显示。 Now if data model data is there it works but in case if there is no model data, I am passing json response so that I can check on razor view in order to avoid null exception. 现在,如果有数据模型数据,它可以工作,但如果没有模型数据,我传递json响应,以便我可以检查剃刀视图,以避免空异常。 My issue is done method in $ajax is not calling plus json response, I don't know where I am doing wrong 我的问题是$ ajax中的方法是不是调用加json响应,我不知道我在哪里做错了

Ajax function 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 Method 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

I have check in debugging, json is called in controller but it alert error in ajax 我已经检查了调试,在控制器中调用了json,但它在ajax中发出警告错误

If your server returns empty string for a json response, jQuery treats it as failed. 如果您的服务器为json响应返回空字符串,jQuery会将其视为失败。 and empty string is considered invalid json. 和空字符串被认为是无效的json。

As per official document here . 根据这里的官方文件。

As of 1.9, an empty string returned for JSON data is considered to be malformed JSON (because it is); 从1.9开始,为JSON数据返回的空字符串被认为是格式错误的JSON(因为它是); this will now throw an error. 这将导致错误。

try below code instead with using .always :- 尝试下面的代码而不是使用.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);
    });

Because . 因为。 done is executed only if everything works successfully so if there will be something wrong .done will not be called. 只有当一切都成功运行时才执行done所以如果出现错误则不会被调用.done将不会被调用。 But the always method will always get triggered regardless your ajax request works or not. 但是无论你的ajax请求是否有效,始终会触发always方法。

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

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

Change code for this: 更改此代码:

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM