Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
Why it value does not send in to ajax post?

I'm a newbie working with ajax. I have a problem while sending the data into ajax post. The output of console.log(obj.Id) and console.log(oke) is 2. Then I tried to send it through data in ajax, but it end up 0 in the controller.

$(function () {
            $("body").on('click', '#btnEdit', function () {
                alert("clicked ok");
                $("#addRowModal").modal("hide");
                var obj = {};
                obj.Id = $(this).attr('data-id');
                oke = $(this).data("id");
                console.log(obj.Id)
                console.log(oke)
 
                $.ajax({
                    url: '@Url.Action("Details", "InvoicePPh")',
                    data: oke,
                    type: 'POST',
                    dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    success: function (response) {
                        alert("sukses");
                    },
                    error: function(response) { 
                        alert("error") 
                    }
                });
            });
        });

And my controller looks like this

[HttpPost]
        public JsonResult Details(int id)
        {
            var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
            InvoicePPh pph = new InvoicePPh();
            pph2326.TaxForm = obj.TaxForm;              
            return Json(pph);
        }

I want the '2' value that passes into my controller, how can I do that? Thank you for your help.

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Please change the data property in ajax part.

          $.ajax({
                url: '@Url.Action("Details", "InvoicePPh")',
                data: { 'id': oke },
                type: 'POST',
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                success: function (response) {
                    alert("sukses");
                },
                error: function(response) { 
                    alert("error") 
                }
            });
over 4 years ago · Santiago Trujillo Denunciar

0

An alternative way to send your data your Controller method using Ajax would be to wrap your data in a JSON object and then send it to the server for processing. The server will be then deserialize your JSON object and you can access the required properties from that process:

$(function () {
    $("body").on('click', '#btnEdit', function () {
        alert("clicked ok");
        $("#addRowModal").modal("hide");
        var obj = {};
        obj.Id = $(this).attr('data-id');
        oke = $(this).data("id");
        console.log(obj.Id)
        console.log(oke)

        var json = {
           oke: oke
        };
        
        $.ajax({
            url: '@Url.Action("Details", "InvoicePPh")',
            data: {'json': JSON.stringify(json)},
            type: 'POST',
            dataType: "json",
            success: function (response) {
                alert("sukses");
            },
            error: function(response) { 
                alert("error") 
            }
        });
    });
});

And your Controller method will be:

using System.Web.Script.Serialization;

[HttpPost]
public JsonResult Details(string json)
{
    var serializer = new JavaScriptSerializer();
    dynamic jsondata = serializer.Deserialize(json, typeof(object));

    //Get your variables here from AJAX call
    var id= Convert.Int32(jsondata["id"]);
    var obj = dbContext.invoicePPhs.FirstOrDefault(s => s.Id == id);
    InvoicePPh pph = new InvoicePPh();
    pph2326.TaxForm = obj.TaxForm;              
    return Json(pph);
}
over 4 years ago · Santiago Trujillo Denunciar

0

If you just need id in your method parameter just change data in ajax to:

 contentType: "application/x-www-form-urlencoded",
 data: { 'id': oke },

id is name of parameter from controller method.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda