0

我想通过 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 发送它,而不进行任何转换,因为如果我有一个包含许多对象的数组,那将非常困难。

4

3 回答 3

2

您可以将它们作为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 实例。

于 2013-09-25T17:07:33.890 回答
0

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

于 2013-09-25T17:29:41.157 回答
0

秘密是: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);
        }
于 2013-09-25T18:38:31.937 回答