如何处理对需要欧洲或美国日期格式的 Web 服务的 Jquery Ajax 调用?
在我的公司,我们有许多客户,其中一些使用 dMyyyy 的欧元日期格式,而我们的其他客户通常使用 MM/dd/yyyy。问题是我正在进行 Jquery ajax 调用:
$.ajax({ url: "/v06/Services/Financials/ChargeEntry.asmx/UpdateProrationAmounts", 数据:JSON2.stringify(DTO), 类型:“帖子”, 内容类型:“应用程序/json”, 数据类型:“json”, 成功:函数(res){...});
构建一个对象,其中包含一些表示日期值的属性。当序列化数据到达 Web 服务时,它无法将欧元日期格式解析为服务器上 DTO 中相应的 Date 属性。我之前在应用程序中没有处理过文化问题,所以我感觉有点像一只迷路的兔子...
基本上目标是将 JSON 日期字符串发送到服务器,无论它代表什么格式,将其传递给使用它进行某些计算的方法,然后将其返回给客户端并以某种方式将其恢复为相同的格式。最好将其作为字符串发送回客户端以绕过 MS Ajax 日期格式,以便解决将其转换为正确格式的客户端问题。有人对如何处理其他方面有任何想法吗?
谢谢!
At my company we have many clients some of which use the Euro date format of d.M.yyyy whereas our other clients typically use MM/dd/yyyy. The problem is I am making a Jquery ajax call:
$.ajax({ url: "/v06/Services/Financials/ChargeEntry.asmx/UpdateProrationAmounts",
data: JSON2.stringify(DTO),
type: "POST",
contentType: "application/json",
dataType: "json",
success: function (res) { ...});
that builds up an object which contains a few properties that represent date values. When the serialized data reaches the web service, it fails to parse out the Euro date format into its corresponding Date property in the DTO on the server. I have not had to deal with culture stuff before in the application so I feel kind of like a lost bunny...
Basically the goal is to send a JSON date string to the server, no matter the format it represents, pass it on to the method that uses it for some calculation, then return it back to the client and somehow put it back into the same format. It would probably be best to send it back to the client as a string to bypass the MS Ajax Date format so that should solve the client side issue of converting it into the correct format. Anyone have any ideas on how to handle the other aspect?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,我回答了我自己的问题。如果有人好奇的话,这就是我所做的。在客户端:
var CultureName = "<%=System.Threading.Thread.CurrentThread.CurrentCulture.Name %>";
这给了我当前线程的文化信息。我在 Ajax 请求中将该信息传回。在网络服务中,我刚刚添加了以下检查:
If glE.CultureName = "en-GB" then
Dim ci As New System.Globalization.CultureInfo("en-GB")
System.Threading.Thread.CurrentThread.CurrentCulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat = New System.Globalization.CultureInfo("de-DE", False).DateTimeFormat
结束如果
这可能不是最好的做事方式,但它达到了我的目的。
Nm, I answered my own question. In case anyone is curious here is what I did. On the client:
var cultureName = "<%=System.Threading.Thread.CurrentThread.CurrentCulture.Name %>";
which gave me the culture info for the current thread. I pass that info back in the Ajax request. And in the webservice I just added the following check:
If glE.CultureName = "en-GB" Then
Dim ci As New System.Globalization.CultureInfo("en-GB")
System.Threading.Thread.CurrentThread.CurrentCulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture = ci
System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat = New System.Globalization.CultureInfo("de-DE", False).DateTimeFormat
End If
It is probably not the best way of doing things but it served my purpose.