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