Search code examples
jqueryajaxposter

What is the difference between Poster post method and jquery post method?


I'm trying to do a post from a mvc3 view and it's not working correctly with my controller, but the same json works fine when I post from Poster

Here is the jquery code

        var lineas = $("#articulosIngresadosTable").getRowData();
        var model = {
            ObraSocialId: $("#idObraSocialTextBox").val(),
            Lineas: lineas
        };

        $.ajax({
            type: 'POST',
            url: '@Url.Action("Nueva", "Factura")',
            data: model,
            success: function (data) { alert(JSON.stringify(data)); },
            dataType: "json"
        });

I double check and the json for the model var is the same that I'm using from Poster

Here is the json:

{"ObraSocialId":"1","Lineas":[{"codigo":"1000","Descripcion":"Articulo 1000","cantidad":"1","importe":"0","descuento":"0","importeDescuento":"0","obrasocial":"","id":"1"},{"codigo":"2000","Descripcion":"Articulo 2000","cantidad":"1","importe":"0","descuento":"0","importeDescuento":"0","obrasocial":"","id":"2"}]}

Thanks in advance!


Solution

  • The problem was the contentType ...

    var lineas = $("#articulosIngresadosTable").getRowData();
    var model = {
        ObraSocialId: $("#idObraSocialTextBox").val(),
        Lineas: lineas
    };
    
    var modelString = JSON.stringify(model);
    
    $.ajax({
        type: 'POST',
        url: '@Url.Action("Nueva", "Factura")',
        data: modelString,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        success: function (data) { alert(JSON.stringify(data)); }
    });