'json form submiting error with status 422

i can't seem to send form submit through server.
before form was submit if any require validate that seem alert working well, but if all required field are value then error status 422 show in console.
i can't figure it out what was causing 422 to be shown if pattern was correct or i am missing something? below is my javascript code:

const name = document.getElementById("name");

const email = document.getElementById("email");

const password = document.getElementById("password");

const first_name = document.getElementById("first_name");

const last_name = document.getElementById("last_name");

const age = document.getElementById("age");

const salary = document.getElementById("salary");

const province = document.getElementById("province");

const register = document.getElementById("register");

function registration(){

fetch("/registration",{

"method": "post",

headers: {

"Content-Type": "application/json"

},

body: JSON.stringify({

"name": name.value,

"email": email.value,

"password": password.value,

"first_name": first_name.value,

"last_name": last_name.value,

"age": age.value,

"salary": salary.value,

"province": province.value,

})

})

.then(function(response) {

response.json().then(function(json){

if( document.myForm.name.value == "" ) {

alert( "Please provide your name!" );

document.myForm.Name.focus() ;

return false;

}

if( document.myForm.password.value == "" ) {

alert( "Please provide your password!" );

document.myForm.password.focus() ;

return false;

}

if( document.myForm.email.value == "" ) {

alert( "Please provide your Email!" );

document.myForm.email.focus() ;

return false;

}

if( document.myForm.first_name.value == "" ) {

alert( "Please provide your first name!" );

document.myForm.first_name.focus() ;

return false;

}

if( document.myForm.last_name.value == "" ) {

alert( "Please provide your last name!" );

document.myForm.last_name.focus() ;

return false;

}

if( document.myForm.age.value == "" ) {

alert( "Please provide your age!" );

document.myForm.age.focus() ;

return false;

}

if( document.myForm.salary.value == "" ) {

alert( "Please provide your salary!" );

document.myForm.salary.focus() ;

return false;

}

if( document.myForm.province.value == "-1" ) {

alert( "Please provide your province!" );

return false;

}

return( true );

alert("registration completed");

window.location.href = "index.html";

})

})

}

register.addEventListener("click", registration);



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source