将 JavaScript 日期发送到 asp.net 服务器

send javascript date to asp.net server

本文关键字:asp net 服务器 JavaScript 日期      更新时间:2023-09-26

我想通过ajax将一个对象发送到 asp.net 服务器。对象中有更多无法识别的日期时间属性。我的意思是参数中的日期时间是默认值 => 0001.01.01

  var a = {start: new Date(), title: "asdf", description: "asdfasdf", allDay: true, end: new Date()};
$.ajax({
            url : m_serverName + "/api/calendar",
            type : "post",
            data : a,
            dataType: 'json',
            success : function() {
                console.log("ok")
            },
            error : function(request, status, error) {
                console.log(request)
            },
            xhrFields : {
                withCredentials : true
            }
        });  

只有一种方法,它是如何工作的。如果我使用 toJSON 并将其作为 JS 发送到服务器。

服务器代码:

 public HttpResponseMessage Post(CalendarEvent calendarEvent)
        {
            int id = -1;
            var httpCookie = HttpContext.Current.Request.Cookies["token"];
            if (httpCookie != null)
                if (!int.TryParse(httpCookie.Value, out id))
                    return Request.CreateResponse(HttpStatusCode.OK, false);
                else
                {
                    int employeeId = service.GetByEmployeeDevice(id).Id;
                    return Request.CreateResponse(HttpStatusCode.OK,
                                                  service.GetEventsById(employeeId));
                }
            return Request.CreateResponse(HttpStatusCode.OK, false);
        }

域模型

 [JsonObject]
    public class CalendarEvent
    {
        [JsonProperty("id")]
        public int Id { get; set; }
        [JsonProperty("employeeId")]
        public int EmployeeId { get; set; }
        [JsonProperty("title")]
        public string Title { get; set; }
        [JsonProperty("description")]
        public string Description { get; set; }
        [JsonProperty("location")]
        public string Location { get; set; }
        [JsonProperty("partner")]
        public string Partner { get; set; }
        [JsonProperty(PropertyName = "allDay")]
        public bool AllDay { get; set; }
        [JsonProperty(PropertyName = "start")]
        public DateTime Start { get; set; }
        [JsonProperty(PropertyName = "end")]
        public DateTime End { get; set; }
        [JsonProperty(PropertyName = "type")]
        public int Type { get; set; }
        [JsonIgnore]
        public Employee Employee { get; set; }
        //navigation property
        //public ICollection<AgentDevice> Devices { get; set; }
    }

我怎么能通过 ajax 发送它,没有任何转换,因为如果我有一个包含这么多对象的数组,那就非常困难了。

您可以将它们作为ISO 8601字符串发送:

var a = {
    start: (new Date()).toISOString(), 
    title: "asdf", 
    description: "asdfasdf", 
    allDay: true, 
    end: (new Date()).toISOString()
};

您在服务器上点击的 ASP.NET Web API 使用 Newtonsoft Json 序列化程序,该序列化程序将能够将它们正确地反序列化为 DateTime 实例。

只需使用类型化列表/数组... List<CalendarEvent>CalendarEvent[]作为数据类型,它应适当地解码 ISO-8601 编码的日期。

秘密是:JsonConvert.DeserializeObject(customerJSON);

public HttpResponseMessage Post([FromBody]String customerJSON)
        {
            if (customerJSON != null)
            {
                int id = -1;
                var httpCookie = HttpContext.Current.Request.Cookies["token"];
                Customer customer = JsonConvert.DeserializeObject<Customer>(customerJSON);

                if (httpCookie != null)
                    if (!int.TryParse(httpCookie.Value, out id))
                        return Request.CreateResponse(HttpStatusCode.OK, false);
                    else
                    {
                        customer.ContactId = employeeService.GetByEmployeeDevice(id).Id;
                        return Request.CreateResponse(HttpStatusCode.OK,
                                                        service.Add(customer));
                    }
            }
            return Request.CreateResponse(HttpStatusCode.OK, false);
        }