I need some help with fetch() POST. The server-side works with Postman, but when I execute a fetch from JavaScript the server endpoint receives an empty object as the req.body.
I have looked at many of the previous questions on this topic with similar symptoms and tried various approaches but I cannot get it to work.
Client-side body:
[
{"gallery":"1","mode":"assign","node":"South Pubs","image":"00000005.jpg"},
{"gallery":"1","mode":"assign","node":"South Pubs","image":"00000006.jpg"}
]
Server side receives empty object.
{}
Server-side Code
const assignPhotos = (req, res) => {
var gallery
var mode
var node
var photo_name
console.log(req.body)
str = JSON.stringify(req.body)
console.log("str: " + str)
lgt = str.length
sp = 1
segs = []
let i = 0
let seg_count = 0
};
Client-side code:
async function postData (url = '', data = {}) {
const options = {
method: "POST",
mode: "no-cors",
cache: "no-cache",
headers: {
"Content-Type": "application/json;charset=utf-8"
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: JSON.stringify(data)
};
const response = await fetch(url, options);
const json = await response.json();
return response.json();
};
postData('http://localhost:5030/assigns',matrix);
I found a solution in question: 67710257. But no explanation was given.