Получить текст с помощью AJAX и MVC - PullRequest
0 голосов
/ 25 июня 2018

У меня есть информация, которую я отправил с помощью jjery ajax на сервер .net, но я не могу получить данные в качестве параметра (любого типа) и изменить их в памяти, а затем преобразовать в json.Я буду благодарен за вашу помощь.

JAVASCRIPT

document.querySelector('input#btnGuardar').onclick = function (e) {
            e.preventDefault();
            var data = $('form#form_boleta').serializeJSON();
            $.ajax({
                type: "post",
                url: "/Comprobante/Factura",
                data: data,
                dataType: 'json',
                contentType: 'application/json; charset=utf-8',
                success: function (result) {
                    if (result === "success") {
                        swal({
                            title: "¿Generar Otro Comprobante?",
                            text: "¡El comprobante se ha generado de manera correcta!",
                            type: "success",
                            showCancelButton: true,
                            confirmButtonClass: 'btn-success',
                            confirmButtonText: 'Si',
                            cancelButtonText: "No",
                            closeOnConfirm: false,
                            closeOnCancel: false
                        },
                            function (isConfirm) {
                                if (isConfirm) {
                                    self.parent.location.reload();
                                } else {
                                    window.location.href = "/Plataforma/Dashboard";
                                }
                            });
                    }
                    else {
                        var mensaje_error = document.getElementById('MensajeError');
                        //$("#MensajeError").fadeTo(1000, 1);

                        //$("#MensajeError").fadeOut(5000);
                        //return false;
                    }
                }
            })
        };

КОНТРОЛЛЕР MVC .NET

public JsonResult Factura(string[] json)//The json parameter appears as Null
        {
            string result;
            if (json != null)
            {
                //Modify the data received json.

                result = "success";
            }
            else
            {
                result = "error";
            }

            return Json(result, JsonRequestBehavior.AllowGet);
        }

1 Ответ

0 голосов
/ 25 июня 2018

Хорошо, сначала вы должны указать имя параметра, которому вы отправляете информацию, в вашем $. Ajax и проанализировать объект "data" в строку Json

$.ajax({
            type: "post",
            url: "/Comprobante/Factura",
            data: {json: JSON.stringify(data)},//JSON.stringify parse a object o json string
            dataType: 'json'
 })

секунда, в вашем контроллере MVC изменяет тип параметра на строку и анализирует строковые данные на тип, который вы хотите.

public JsonResult Factura(string json)//The json parameter appears as Null
    {
        string result;
        string[] data = JsonConvert.DeserializeObject<string[]>(json);
        if (data != null)
        {
            //Modify the data received json.

            result = "success";
        }
        else
        {
            result = "error";
        }

        return Json(result, JsonRequestBehavior.AllowGet);
    }
...