this is my project: When the login button on the login page is clicked, the values entered in the username and password entries will be received with js and a request will be sent to the service at https://fakestoreapi.com/docs#a-login. When a response comes from this service, a redirection will be made to the dashboard from the login page. I’m getting this error in the console "TypeError: JSON.stringfy is not a function"
document.addEventListener("DOMContentLoaded", function() {
loginform.addEventListener("submit", function(event) {
event.preventDefault()
const username = document.getElementById("username").value;
const password = document.getElementById("password").value;
const data = {
username,
password
}
console.log(JSON.stringfy(data))
fetch('https://fakestoreapi.com/auth/login', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.log(err.message));
});
});
2
Answers
There is a typing mistake. Use
JSON.stringify(data)
instead ofJSON.stringfy(data)
Recommendation: Use Typescript to resolve such errors instantly while writing the code.
You have typo error
stringfy != stringify
Use JSON.stringify(json)