【问题标题】:PUT request could not be processed by the server due to invalid syntax (ASP.NET)由于语法无效 (ASP.NET),服务器无法处理 PUT 请求
【发布时间】:2018-12-27 10:44:59
【问题描述】:

我正在尝试使用 ajax 发出 PUT 请求以更新 Rental 对象。这是ajax请求。请注意,img.attr("data-rental-id") 工作正常。

$("#rentals").on("click", ".js-update", function () {
                    var img = $(this);
                    var vm = {};
                    vm.id = parseInt(img.attr("data-rental-id"));
                    vm.dateReturned = new Date(); 
                    bootbox.confirm("Are you sure you want to mark this rental as returned?", function (result) {
                        if (result) {
                            $.ajax({
                                url: "/api/newRentals/" + img.attr("data-rental-id"),
                                type: "PUT",
                                data: vm,
                                success: function () {
                                    alert('Updated');
                                }
                            });
                        }
                    });
                });

这是我的 API 更新方法:

[HttpPut]
public IHttpActionResult UpdateRental(UpdatedRentalDto UpdatedRentalDto)
{
    if (!ModelState.IsValid)
        return BadRequest();

    var rentalInDb = _context.Rentals.SingleOrDefault(r => r.Id == UpdatedRentalDto.Id);

    if (rentalInDb == null)
        return NotFound();

    Mapper.Map(UpdatedRentalDto, rentalInDb);

    _context.SaveChanges();

    return Ok();
}

最后是我的 DTO:

public class UpdatedRentalDto
{
    [Required]
    public int Id { get; set; }

    public DateTime DateReturned { get; set; }
}

理论上一切似乎都很好,但是,当我尝试运行它时,我得到:

HTTP400: BAD REQUEST - The request could not be processed by the server due to invalid syntax.
(XHR)PUT - http://localhost:57265/api/newRentals/1

这是否意味着数据变量 vm 有问题?有什么错误?

【问题讨论】:

  • 我相信这是区分大小写的,试着把你的 vm 变量写成这样: vm.Id = parseInt(img.attr("data-rental-id")); vm.DateReturned = new Date();
  • 我使用 settings.ContractResolver = new CamelCasePropertyNamesContractResolver();所以不应该。但我试过了,它没有用。不过感谢您的建议!
  • ¿你能输入你的 ajax 调用: contentType: json 吗?可能数据是作为文本/纯文本发送的,这就是您的 API 无法读取它的原因。
  • 还将您的数据以 JSON.stringify(vm) 形式发送到 ajax 调用的“数据”键中
  • 服务器拒绝为请求提供服务,因为请求实体的格式不受请求方法的请求资源支持。

标签: javascript c# .net ajax api


【解决方案1】:

我认为您的问题在于 javascrip - /C# 的日期时间格式。如果您在 javascript 中执行新日期(在我的浏览器版本中:2018 年 7 月 19 日星期四 12:31:40 GMT+0200(浪漫夏令时间)。您将其发送到服务器,它可能无法解析为 DateTime c#的字段。

1) 将 NewtonSoft.Json 的引用添加到您的 c# 项目中

2) 添加一个 DateFormatConverter 类,这是我的示例

public class DateFormatConverter : IsoDateTimeConverter
    {
        public DateFormatConverter(string format)
        {
            DateTimeFormat = format;
            Culture = new CultureInfo("nl-BE"); //use this to have CET time !
        }
    }

2) 在你的 dto 中,你可以在你可以定义的地方使用这个属性(使用你想要的日期时间格式)

[JsonConverter(typeof(DateFormatConverter), "dd/MM/yyyy HH:mm")]

3) 在 javascript 中添加对 moment.js(一个非常流行的日期/时间处理库)的引用

4) 使用momentJS的格式化功能在发送到服务器之前格式化你的日期时间

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多