How to change the data type in the $ .post request in jQuery?

1
var envio = $.post("processamento/busca.php", { 
            unidade: $("#unidade").val()
            })

I have this request, I would like to change the datatype to json, in ajax has a datatype parameter, but I do not know how to use it in $ .post, if I can use it ... I know it's possible do with $ .ajax, but I have so many codes in $ .post that it would be too much trouble to change everything.

Thank you

    
asked by anonymous 25.02.2016 / 20:57

2 answers

1

With the $.post(url, data, success, dataType) method, you send the parameters like this:

$.post('processamento/busca.php', { 
         unidade: $("#unidade").val() 
     }, function (response) {
         console.log(response); // aqui você vai tratar o JSON recebido
     }, 'json');

This form is a shortcut to the $.ajax method shown in another response.

    
25.02.2016 / 21:40
2

The $.post is a simplification in the normal ajax syntax (I'll display it below), if I'm not mistaken, it's not possible to pass this option on only one request, to solve the problem I advise you to use it in the following way.

$.ajax ({
    url: url,
    type: "POST",
    data: JSON.stringify({data:"test"}),
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(){
        //
    }
});
    
25.02.2016 / 21:07