问题描述
我目前正在开发一个项目,我通过 ajax 将 .Net 类型发送到客户端应用程序.我对被序列化并设置给客户端的对象没有任何问题.
I am currently working on a project where I am sending a .Net type via ajax to a client application via ajax. I have no issues with the object being serialized and set to the client.
当我获取完全相同的对象并通过 Web 方法将其发回服务器时遇到问题,并出现以下错误:/Date(1373950800000)/不是 DateTime 的有效值.
这很烦人,因为这是微软给我的方式,但这不是重点.
I run into issues when I take the exactly same object and post it back to the server via a web method with the following error: /Date(1373950800000)/ is not a valid value for DateTime.
Which is pretty annoying as that is how Microsoft gave it to me, but that's besides the point.
有没有人可以快速解决这个问题?我想要一种无缝方式,无需在从 ajax 调用返回对象之前立即更改对象.
Does anyone have a quick fix for this? I want a seamless way this can be accomplished without having to change the object right before returning it from the ajax call.
推荐答案
您的问题归结为您使用的服务器端 JavaScript 序列化程序;JsonDataContractSerializer
(ASP.NET MVC 的默认序列化器)或 NewtonSoft Json Serializer
(ASP.NET Web API 的默认序列化器).
Your issue comes down to the server-side JavaScript serializer you are using; either JsonDataContractSerializer
(default serializer for ASP.NET MVC) or NewtonSoft Json Serializer
(default serializer for ASP.NET Web API).
有关此日期修改问题的直观示例以及可能的解决方案,请查看 ASP.NET MVC 和 Web API 中的 JSON 日期不同.
For a visual example of this date mangling issue as well as possible solutions, check out JSON Dates are Different in ASP.NET MVC and Web API.
这篇关于从 .Net Web 方法修复 JSON 日期序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!