创建了一个 web api [HttpPut] 方法。从本地主机测试时,数据正在更新。访问 api(托管在 nginx - aws ec2 中)时,会引发 403 错误。这个api是在asp.net core web-api中开发的。
这个 api 是通过 JQuery ajax 调用的。
控制器:
[Authorize]
[Route("api/v1/[controller]")]
[ApiController]
public class EventSchedulerController : ControllerBase
{
[HttpPut]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
[Route("UpdateEvent")]
public async Task<IActionResult> UpdateEvent(EventSchedulerDTO evt)
{
string message = string.Empty;
if (string.IsNullOrEmpty(evt.Id))
{
return BadRequest();
}
try
{
var existingEventData = _db.EventSchedulers.FirstOrDefault(m => m.Id == evt.Id);
existingEventData.Title = evt.Title;
existingEventData.Description = evt.Description;
existingEventData.EventStartDateTime = evt.EventStartDateTime;
existingEventData.EventEndDateTime = evt.EventEndDateTime;
_db.EventSchedulers.Update(existingEventData);
await _db.SaveChangesAsync();
return Ok(new { Status = "Success", Message = "Data successfully updated" });
}
catch (Exception ex)
{
return StatusCode(StatusCodes.Status500InternalServerError, ex);
}
}
}
查询:
$("#btnSaveSchedule").click(function () {
var eventSchedulerData = {
"id": $(this).data("event-id"),
"title": $("#txtTitle").val(),
"description": $("#txtDescription").val(),
"eventStartDateTime": new Date($("#txtStartDateTime").val()).toISOString(),
"eventEndDateTime": new Date($("#txtEndDateTime").val()).toISOString(),
};
$.ajax({
url: '/api/v1/EventScheduler/UpdateEvent',
type: 'put',
data: JSON.stringify(eventSchedulerData),
dataType: 'json',
contentType: 'application/json',
success: function (data, textStatus, xhr) {
}
});
});