I’m trying to remove jQuery from some code. I only use it for POST operations so I want to drop it and use fetch() instead. But I can’t get fetch() to work using the same data. The php file is working OK, it is just not receiving the data
This sets up the test data for both test cases below:
var toPostObj = new(Object);
toPostObj.action = "update+file";
toPostObj.arrays = [["2020-12-28", "23:20:56", "Trying from ztest", "9.jpg"]];
This works using jQuery:
$.post('toMariaDB.php', { // url
data: toPostObj
}, function(data2, status, jqXHR) {
console.log ("data2",data2);
});
This does not work using fetch():
fetch("toMariaDB.php", {
method: "POST",
body: toPostObj, // using JSON.stringify(toPostObj) also doesn't work
headers: { "Content-type": "application/text; charset=UTF-8" }
})
.then(response => response.text())
.then(text => console.log(text))//;
.catch(error => {
console.error(error)
})
For debugging purposes, toMariaDB.php writes out a log file of the data it receives and any other messages from toMariaDB.
Running the jQuery code writes this to the log file:
toMariaDB: I've ARRIVED
in toMariaDB 1=>Array
(
[action] => update+file
[arrays] => Array
(
[0] => Array
(
[0] => 2020-12-28
[1] => 23:20:56
[2] => Trying from ztest
[3] => 9.jpg
)
)
)
which is what toMariaDB.php expects.
But the fetch() version writes this:
toMariaDB: I've ARRIVED
in toMariaDB 1=>
The result for fetch() is the same whether I use
body: toPostObj,
or
body: JSON.stringify(toPostObj),
I’ve used
headers: { "Content-type": "application/text; charset=UTF-8" }
since toMariaDB.php returns text and, as I understand it, headers describes what is returned
but just in case I had misunderstood, I tried
headers: { "Content-type": "application/json; charset=UTF-8" }
as well, but that didn’t work either.
How can I format the body so that it arrives at toMariaDB.php in the same form as with jQuery? I.e.
toPostObj.action = "update+file";
toPostObj.arrays = [["2020-12-28", "23:20:56", "Trying from ztest", "9.jpg"]];
Thanks for any help.
EDIT
As suggested by @T.J.Crowder, (thanks for pointing me at that) here’s what the Network tab shows as the Request Payload when running with jQuery:
data[action]: update+file
data[arrays][0][]: 2020-12-28
data[arrays][0][]: 23:20:56
data[arrays][0][]: Trying from ztest
data[arrays][0][]: 9.jpg
I don’t understand why these don’t show as data[arrays][0][0], etc., but it works.
(It’s a 2D array because toMariaDB.php has to be able to process multiple arrays.)
With fetch(), the Network tab Request Payload shows:
[object Object]
2
Answers
From the documentation we can see that…
(It doesn’t say so, but it does it recursively.)
Your code is sending an object with a single top-level property called
data
whose value is yourtoPostObj
, which in turn has properties with string and array values. It ends up sending a POST body that looks like this:…which is these parameters:
You can replicate that with a
URLSearchParams
object like this:URLSearchParams
will handle the URI-escaping etc. for you.I think you’d have the best experience using this:
And doing
json_decode
on the PHP side.If you want to exactly replicate what jQuery does, you’ll have to look into the network tab as it can differ… but most likely it’s using
application/x-www-form-urlencoded
as the content type. Best way to replicate that is to populate aFormData
object which is still quite a hassle when you have arrays, especially nested arrays.