WebApi HttpPut在本地主机中运行良好.在服务器中它不工作.它抛出403



创建了一个web api[HttpPut]方法。从本地主机进行测试时,数据正在更新。当访问api(托管在nginx-aws ec2中(时,它抛出403错误。这个api是在asp.net核心web api中开发的。

这个api是通过JQueryajax调用的。

控制器:

[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);
}
}
}

JQuery:

$("#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) {
}
});
});

从控制器中删除[授权],否则您需要向ajax请求添加一个令牌。

之后尝试代码:

$("#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: eventSchedulerData,
success: function (data, textStatus, xhr) {
}
});
});

最新更新