Все работает в чванстве, но если я хочу сделать запрос в ajax, я получаю ошибку: < /p>
" Звоните: < /p>
Код: Выделить всё
function sendTodoTitle() {
$.ajax({
url: url + "/api/todo/sendTitle",
type: "POST",
dataType: "JSON",
data: JSON.stringify({todoItem: textIn.val()}),
success: function(data) {
valData(data);
}
});
}
< /code>
URL действительно работает, поэтому это не проблема из -за URL.
textIn
my Controller:
Код: Выделить всё
[Route("api/[controller]")]
[ApiController]
public class TodoController : ControllerBase {
InFSTodo fs = new InFSTodo();
[HttpPost]
public ActionResult Post() {
return Ok(fs.GetJSON());
}
[HttpPost("sendTitle")]
public ActionResult Post([FromBody] String todoItem) {
var list = fs.Get();
list.Add(new Todo(list.Count, todoItem));
fs.Store(list);
return Ok(list.ToJson());
}
< /code>
Из предыдущих вопросов, которые задавали здесь, я получил следующее: < /p>
Для примитивных данных данных Цените любую помощь! < /p>
Редактировать: < /p>
infstodo.cs:
public class InFSTodo {
string fileName = "todo.json";
///
/// Store the JSON in file
///
///
The list of todos to be stored
public void Store(List list) {
string json = list.ToJson(); // ToJson() item.Id == id));
fs.Store(list);
return Ok(list.ToJson());
}
[HttpGet("{id}")]
public ActionResult PostDone(int id) {
var list = fs.Get();
var todo = list.Find(item => item.Id == id);
if (todo != null)
todo.WasDone = true;
else
return StatusCode(422); // 422 Unprocessable Entity
fs.Store(list);
return Ok(list.ToJson());
}
}
< /code>
todo.cs:
[Serializable]
public class Todo {
public int Id { get; private set; }
public String Title { get; set; }
public bool WasDone { get; set; }
public Todo(int id, String title) {
Id = id;
Title = title;
WasDone = false;
}
public void ChangeID(Todo second) {
int id = Id;
Id = second.Id;
second.Id = id;
}
}
Ошибка в postman:
Подробнее здесь: https://stackoverflow.com/questions/683 ... -error-415