将 JavaScript 日期发送到 asp.net 服务器
send javascript date to asp.net server
我想通过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);
}
相关文章:
- 借助asp.net验证或java脚本对多个文本进行验证
- 将Javascript数组发送到控制器ASP.NET MVC
- ASP.NET通过单击JavaScript按钮触发c#事件
- 文本框不是从Javascript/Asp.net中的对话框中打印出来的
- 从javascript调用asp.net codebehind函数
- 使用javascript的Asp.net内容占位符
- 获取ASP.NET Ajax Timer状态
- ASP.NET网络摄像头显示
- 如何在只能有一个asp.net表单的主页上从asp.net页面中的javascript中获取值
- 如何将乳胶配方奶粉图像保存到Asp.net中的文件夹中
- asp.net,包括iss上的javascript slow
- 选择不在GridView-ASP.NET中激发
- 什么'是在asp.net MVC中将本地化的resources.resx文件转换为javascript文件的有效
- 使用ASP.NET将谷歌地图添加到项目中,并从数据库中检索位置
- 有什么“;错误的”;将javascript放在asp.net表单上,而不是放在单独的文件中
- 将Javascript日期转换为ASP.NET日期格式
- C#asp.net mvc Set CheckBoxFor已检查的具有Model Value的属性
- 在asp.net中确认是或否消息
- ASP.NET MVC 3-在ajax调用后,重定向到新页面或生成页面刷新
- asp.net中的验证工具