Jan-07-2022, 03:22 PM
A little off the scope of Python but I'm trying to get things done in the frontend before integrating the Django backend.
I have tried different header objects to this simple html page from where I'm to send Post request to an API endpoint. It's a form data of username and the password. I keep getting
I have tried different header objects to this simple html page from where I'm to send Post request to an API endpoint. It's a form data of username and the password. I keep getting
Error:POST https://some/api/endpoint/ 415 (Unsupported Media Type) error message.<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="styly.css">
<title>Login</title>
</head>
<body>
<form>
<input type="text" id="username" name="user_name" placeholder="Username"> <br> <br>
<input type="password" id="password" name="password" placeholder="Password"> <br> <br>
<input type="submit" value="Submit">
</form>
<script>
const url = "https://some/apo/endpoint/";
const formEl = document.querySelector("form");
formEl.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(formEl);
const formDataSerialized = Object.fromEntries(formData);
const jsonObject = {
...formDataSerialized,
sendToSelf: formDataSerialized.sendToSelf ? true : false,
};
try {
const response = await fetch(url, {
method: "POST",
body: JSON.stringify(jsonObject),
headers: {
"Content-Type": "application/json",
},
mode: "no-cors"
});
const json = await response.json();
console.log(json);
} catch (e) {
console.error(e);
alert("there as an error");
}
});
</script>
</body>
</html>
