Ajax request with JSON type parameter using Jquery 1.8.2

Asked

Viewed 560 times

0

I am trying to make an ajax request that sends a list of objects, but it seems that in this version of Jquery 1.8.2 (I cannot update because the system is legacy and has many features using this version), it seems that it does not recognize the

contenttype: "application/json" or data: JSON.stringify(obj) when I use this code the request does not arrive at the server, and when I take the contenttype the object arrives null in my controller:

 $("#results :input").change(function () {
        var type = $(this).attr("type")
        var id = $(this).attr('name')
        var obj = {}

        if (type == 'text')
            obj = { Unidades: listaGlobal, Id: id, Valor: $(this).val(), Type: type }

        else if (type == 'checkbox')
            obj = { Unidades: listaGlobal, Id: id, Selecionado: $(this).is(':checked'), Type: type }

        // JQuery.ajaxSettings.traditional = true
        $.ajax({
            url: "/parametrizacao/SetaValor",
            data: JSON.stringify(obj),
            type: 'POST',
            contentType: "application/json",
            cache: false,
            success: function (result) {
                $('#results').html(result)
            }
        })


    })
  • Jhonatas, this has nothing to do with the version of jQyery. Version 1.8.2 supports contenttype, which includes no validation, you could even use contentType: 'teste123' that jQuery would pass on, simply adding to Content-Type of the request header. Your problem must be another, the object format, or the lack of setting in the ajax call dataType: "json", make a test adding this and make sure your object is compatible with what the method is waiting for

  • @Ricardopunctual ah yes I understood what you said, I put the dataType: "json" and the request arrived in my controller, the only problem is that the attribute Units (which is a list) was sent without the values in the properties, you know what might be?

  • When I give console.log in the list I see that it is correct, but it does not arrive like this on the server

  • 1

    What you have in this object, a list of other objects or only simple values, like numbers or strings, like: {['um','dois']}? If it is an object, it has to be matching exactly with what it has on the controller object

  • is a list of objects, check all fields are equal, only it comes with the default values (empty quotes for strings, and zero for int)

  • To be sure, go to this link http://json2csharp.com/ put your json there to see the expected class structure

  • This identical @Ricardopunctual did not understand yet why it is not passing the values to the objects.

  • Without seeing the class code and the full json is difficult to opine, but if the structure is the same, it should be something simple. Try sending this list with only one object with a very basic value. For example if it is string property, "a" and see if it works, then add the items slowly. If it is not, it can be the syntax, the marry in the name of a property for example, for sure it is some detail

  • @Ricardopunctual I discovered what was expensive, it seems that JSON.stringfy has a certain conversion limit, and I had a list with 83 objects and so I was giving in, at first I found it strange that one had worked only that this time I was using a list with 50 items only

  • I understood, although it does not seem to me to be a value that will generate this problem, you have to see the whole object, it can be a limitation of the string. If you use the [F12] and inspect the requisition, you can see the object being sent and see if truncated the cut something just to clear the doubt

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.