I am using Fetch and I want to pass the GET and POST values exactly as JQuery does, I have illustrative images:
My data to send:
and using Jquery GET send the Object as plain text using encodeURIComponent:
$.ajax({
data: obj,
url: 'ajax.php',
method: "get",
success: function(r) {
}
});
http://localhost/index/Fede/ajax.php?obj%5B0%5D=4123&obj1%5Bv1%5D=1&obj1%5Bv2%5D=4&obj1%5Bv3%5D%5B%5D=123&obj1%5Bv3%5D%5B1%5D%5Bv11%5D=v11&obj2%5Bvv1%5D=2&obj2%5Barr%5D%5B0%5D%5Barrv%5D=123&obj2%5Barr%5D%5B%5D=312
How can I send this object with fetch to the Jquery Ajax style in the form of plain text? Like this (I used the same ajax GET String to show you how I want to send the data):
fetch('ajax.php?obj[0]=4123&obj1[v1]=1&obj1[v2]=4&obj1[v3][]=123&obj1[v3][1][v11]=v11&obj2[vv1]=2&obj2[arr][0][arrv]=123&obj2[arr][]=312')
.then(r => /text\/html/.test(r.headers.get('content-type')) ? r.text() : (/json/.test(r.headers.get('content-type')) ? r.json() : r.text()))
.then(r => console.log(r))
.catch(e => console.error(e));
http://localhost/index/Fede/ajax.php?obj%5B0%5D=4123&obj1%5Bv1%5D=1&obj1%5Bv2%5D=4&obj1%5Bv3%5D%5B%5D=123&obj1%5Bv3%5D%5B1%5D%5Bv11%5D=v11&obj2%5Bvv1%5D=2&obj2%5Barr%5D%5B0%5D%5Barrv%5D=123&obj2%5Barr%5D%5B%5D=312
In both cases the server receives it and gives answers. The reason for this is that I am deprecating Jquery, I want all my scripts to be nativeJS.
Ready friends, I did not give up, I designed a function that converts an object to plain text without the need to use JSON.stringify and I share it with you just in case someone needs it:
You only need to use Fetch and the URL where you will receive the data: