我如何从控制器发送http到我的ajax?

时间:2016-08-09 19:50:55

标签: javascript ajax http asp.net-mvc-5

如何从控制器向我的ajax发送Http ok和错误? 例如httpstatuscodeokay和httpstatuscodebadrequest 因为当我把检查放在我的浏览器中时说impresion.js 304没有修改



$(document).ready(function () {
        $("#confirmacion-validar-impresion").click(function (e) {
            e.preventDefault();
            var form = $("form#ValidarImpresion").serializeArray();

            $.ajax({
                type: "POST",
                dataType: "json",
                url: '/FotoCarnet/ValidarImpresion',
                data: form,
                success: function (response) {
                        $("#modal-confirmacion-carne-impreso").modal('toggle')
                        timedRefresh();
                },
                error: function (response) {
                        alert("Carné no impreso")
                }
            });

            $("#confirmacion-reimprimir-carnet-modal").modal('hide');
            window.onbeforeunload = false;
        });
    });

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ValidarImpresion(FormCollection form)
        {
   //some query
            if (queryBeneficiario.Count() > 0)
            {
                beneficiario = queryBeneficiario.First();
                beneficiario.impreso = true;
                _dbContext.SaveChanges();
                return Json(new {sucess= true , HttpStatusCode.OK});
            }
            else
            {
                Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
                return Json(new { success = false });
            }
        }

<div class="modal fade" id="confirmacion-reimprimir-carnet-modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title">Validar Impresion</h4>
            </div>
            <div class="modal-body">
                <p><h4>El Carné fue impreso correctamente?</h4></p>
            </div>
            <div class="modal-footer">
                <form id="ValidarImpresion" name="ValidarImpresion" action="#" method="post">
                    @Html.AntiForgeryToken()
                    <input type="hidden" name="cedula" value="@ViewBag.cedula" />
                    <button id="confirmacion-validar-impresion" type="submit" class="btn btn-success">Sí</button>
                    <button id="impreso-incorrectamente" type="button" class="btn btn-danger" data-dismiss="modal">No</button>
                </form>
            </div>
        </div>
    </div>
</div>
&#13;
&#13;
&#13; 任何想法?

1 个答案:

答案 0 :(得分:0)

你可以在错误的操作中返回HttpStatusCodeResult(如500)。

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ValidarImpresion(FormCollection form)
        {
   //some query
            if (queryBeneficiario.Count() > 0)
            {
                beneficiario = queryBeneficiario.First();
                beneficiario.impreso = true;
                _dbContext.SaveChanges();
                return Json(new {sucess= true , HttpStatusCode.OK});
            }
            else
            {
                Response.StatusCode = (int)System.Net.HttpStatusCode.BadRequest;
                return new HttpStatusCodeResult(500);
            }
        }