I've made a POST request with the react Fetch API, it works as desired, except for the fact that it replaces the url of the current page.
Post Function:
postNote(note) {
console.log('Posting note');
fetch('http://localhost:9000/notes/create', {
mode: 'cors',
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(note)
})
}
When this function execute, it replace my url with:
http://localhost:3000/?name=nameExample&description=descExample
I don't know why this happens, as i used this API before, and i didn't have this problem. If anyone can give me any help, i would appreciate it.
Thanks in advance, have a great week!
Edit 1:
I create the note object like this:
const newNote = {id: 0, name: note.name, description: note.description, author_id: note.author_id, author: note.author };
Edit 2:
The postNote function is triggered by this function:
addNote = note => {
const newNote = {id: 0, name: note.name, description: note.description, author_id: note.author_id, author: note.author };
this.setState({
notas: [...this.state.notas, newNote]
});
console.log(newNote);
this.postNote(newNote);
}
Edit 3:
<button className="btn btn-primary" onClick={() => {
let newNote = { id: 0, name: this.name, description: this.description, author_id: this.author_id, author: this.author }
noteContainer.addNote(newNote)
}
}>
Save
</button>
<form>submit? Are you preventing the default?