I am using a script to send transactions from my card website to localhost, which is accurately receiving it and also successfully sends a json response but after that console.log isn’t displaying it at all. I can check the response on network tab in dev tools but just curious what’s the reason for not printing json response.
async function sendData(username, transactions) {
let apiUrl = "http://localhost:3100/api";
let postOptions = {
method: "POST",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: JSON.stringify({ username, transactions }),
};
try {
let response = await fetch(apiUrl, postOptions);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
let serverMessage = await response.json();
console.log(serverMessage);
} catch (error) {
console.log(error.message);
}
}
I even tried without async as following but still nothing printed.
function sendData(username, transactions) {
let apiUrl = "http://localhost:3100/api";
let postOptions = {
method: "POST",
cache: "no-cache",
headers: {
"Content-Type": "application/json",
},
redirect: "follow",
referrerPolicy: "no-referrer",
body: JSON.stringify({ username, transactions }),
};
fetch(apiUrl, postOptions)
.then((res) => res.json())
.then((msg) => console.log(msg))
.catch((e) => console.log(e));
}
One Clarification:
When I run the same code (with some triggering front end of course) in a JS injector chrome extension (e.g; scripty), it then prints the response, but not when I call it manually in console.