im begginer in asp+react dev and im trying to make CRUD poroject. Im stuck at the DELETE request. Im getting 404 error when im trying to delete note. But in swagger everything works fine and i have access to note throug the URL, in CORS all methods allowed, axios function getting the correct id.
Heres ASP delete-func
[HttpDelete("{Id}")]
public async Task<IActionResult> Delete(DeleteNoteRequest request, CancellationToken ct)
{
var note = await _dbContext.Notes.FindAsync(request.Id);
if (note == null)
{
return NotFound();
}
_dbContext.Notes.Remove(note);
await _dbContext.SaveChangesAsync(ct);
return NoContent();
}
axios-func
export const deleteNote = async (Id) => {
try {
var response = await axios.delete(`http://localhost:5109/notes/${Id}`, {
data: {foo: 'bar'}
});
console.log(response.status, response.statusText)
return response.status;
} catch (e) {
console.error(e);
}
App.jsx
const onDelete = async (id) => {
await deleteNote(id);
let notes = await fetchNotes(filter);
setNotes(notes);
};
return (
<section>
<div className="main">
<NoteForm filter={filter} setFilter={setFilter} onCreate={onCreate} />
<ul>
{notes.map((n) => (
<li key={n.id}>
<Note
id={n.id}
title={n.title}
description={n.description}
createdAt={n.createdAt}
onDelete={onDelete}
/>
</li>
))}
</ul>
</div>
</section>
);
Note.jsx
import React from "react";
import "./Note.css";
import moment from "moment";
const Note = ({ id, title, description, createdAt, onDelete }) => {
const removeNote = (e) => {
e.preventDefault();
onDelete(id);
console.log(id);
};
return (
<div className="card">
<h2>{title}</h2>
<div className="note-body">
<p className="description">{description}</p>
<p className="date">{moment(createdAt).format("DD/MM/YYYY")}</p>
<button onClick={removeNote} className="delete-bttn">Удалить</button>
<button className="edit-bttn">
Редактировать
</button>
</div>
</div>
);
};
export default Note;
I read that axios doenst support content type and add a ‘trick’ in axios function. Without it i get 415 error
user26491209 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.