将javascript日期发送到asp.net服务器
我想通过ajax将一个对象发送到asp.net服务器。对象中有更多无法识别的datetime属性。我的意思是参数中的日期时间是默认值=>0001.01.01将javascript日期发送到asp.net服务器,javascript,jquery,ajax,json,asp.net-web-api,Javascript,Jquery,Ajax,Json,Asp.net Web Api,我想通过ajax将一个对象发送到asp.net服务器。对象中有更多无法识别的datetime属性。我的意思是参数中的日期时间是默认值=>0001.01.01 var a = {start: new Date(), title: "asdf", description: "asdfasdf", allDay: true, end: new Date()}; $.ajax({ url : m_serverName + "/api/calendar",
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; }
}
[JsonObject]
公共类日历事件
{
[JsonProperty(“id”)]
公共int Id{get;set;}
[JsonProperty(“员工ID”)]
public int EmployeeId{get;set;}
[JsonProperty(“所有权”)]
公共字符串标题{get;set;}
[JsonProperty(“描述”)]
公共字符串说明{get;set;}
[JsonProperty(“位置”)]
公共字符串位置{get;set;}
[JsonProperty(“合伙人”)]
公共字符串伙伴{get;set;}
[JsonProperty(PropertyName=“allDay”)]
公共布尔全天{get;set;}
[JsonProperty(PropertyName=“start”)]
公共日期时间开始{get;set;}
[JsonProperty(PropertyName=“end”)]
公共日期时间结束{get;set;}
[JsonProperty(PropertyName=“type”)]
公共int类型{get;set;}
[JsonIgnore]
公共雇员雇员{get;set;}
//导航属性
//公共ICollection设备{get;set;}
}
我如何通过ajax发送,而不进行任何转换,因为如果我有一个包含somany对象的数组,这非常困难。您可以将它们作为字符串发送:
您在服务器上使用的ASP.NET Web API使用Newtonsoft Json序列化程序,该程序将能够正确地将它们反序列化为DateTime实例。只需使用类型化列表/数组
将
或CalendarEvent[]列为您的数据类型,它应该适当地解码ISO-8601编码的日期。秘诀是:JsonConvert.DeserializeObject(customerJSON)
public HttpResponseMessage Post([FromBody]字符串customerJSON)
{
if(customerJSON!=null)
{
int id=-1;
var httpCookie=HttpContext.Current.Request.Cookies[“token”];
Customer=JsonConvert.DeserializeObject(customerJSON);
if(httpCookie!=null)
如果(!int.TryParse(httpCookie.Value,out id))
return Request.CreateResponse(HttpStatusCode.OK,false);
其他的
{
customer.ContactId=employeeService.GetByEmployeeDevice(id).id;
返回请求.CreateResponse(HttpStatusCode.OK,
服务。添加(客户));
}
}
return Request.CreateResponse(HttpStatusCode.OK,false);
}
我可以使用toJSON方法,服务器会识别它,但是如果我有一个包含数千个项目的数组。我必须转换所有数组项的all date属性吗?我理解,但在我必须将date对象转换为JSON或ISOString之前,不是吗?在JavaScript中,当您使用JSON序列化某些内容时。stringify(…)默认情况是使用UTC/GMT作为ISO-8601样式的datetime序列化日期时间实例。
var a = {
start: (new Date()).toISOString(),
title: "asdf",
description: "asdfasdf",
allDay: true,
end: (new Date()).toISOString()
};
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);
}