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

Asked

Viewed 972 times

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 you have a datatype parameter, but I don’t know how to use it in $.post, if you can use it... I know it’s possible to do with $.ajax, but I have so many codes in $.post that it would be too much trouble to change everything.

Thank you

2 answers

2

The $.post is a simplification of the normal ajax syntax (I will show it below), if I am not mistaken it is not possible to pass this option in just one request, to solve the problem I advise you to use it in the way below.

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

1


With the method $.post(url, data, success, dataType), 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 method $.ajax shown in another answer.

Browser other questions tagged

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