我想通過ajax發送一個對象到asp.net服務器。對象中有更多的datetime屬性不被識別。我的意思是參數中的日期時間是默認值=> 0001.01.01發送javascript日期到asp.net服務器
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發送此,沒有任何convertation,因爲如果我有somany對象的數組是很困難的。
我可以使用toJSON方法,並且服務器會識別它,但是如果我有一個包含數千個項目的數組。我是否必須轉換所有數組項目的所有日期屬性? – user1693057
是的,你必須。 –
noo,whyyy?我不想相信它 – user1693057