Error 422 when working with fetch
I don't understand why error 422 crashes. Here is my code for the request:
function data() {
let self = this;
self.apiAdd = api;
self.add = function (newData) {
fetch(self.apiAdd, {method: 'POST'})
.then(fulfilled => fulfilled.json())
.then(fulfilled => {
console.log('Успех', newData);
})
.catch(error => console.log('Причина ошибки: ' + error));
}
}
Code for data collection:
finalAdd.addEventListener('click', function () {
let newPerson = {
Name: name.value,
Email: email.value,
};
let newPersonJson = JSON.stringify(newPerson);
new data().add(newPersonJson);
});
0
1 answers
I will assume that the problem is that you pass newData
, but when POST
is requested, you do not specify them anywhere, and fetch
requires the body
parameter. Specify it in fetch
:
fetch(self.apiAdd, {
method: 'POST',
body: newData,
headers: {
'Content-Type': 'application/json'
},
})...
0
Author: Dmitrii Sedov, 2019-09-25 11:56:31