0

我创建了以下接受 .NETSystem.DateTime值作为输入参数的 JSON WCF 服务:

[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
ReclaimedH2OMetric GetCurrentReclaimedH2OMetric(System.DateTime currentDate);

jQuery当我尝试在我的网页中使用该服务时,我收到以下错误:

服务器在处理请求时遇到错误。异常消息是“SqlDateTime 溢出。必须在 1753 年 1 月 1 日上午 12:00:00 到 9999 年 12 月 31 日晚上 11:59:59 之间。

这是我的 jQuery 代码:

var rawResults;
var currentDate = new Date('10/1/2012');
var jsonDate = '\\/Date(' + currentDate.getTime() + ')\\/';

$.ajax(
{
    async: false,
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://www.mydomain.com/Dashboard_WaterResources/WCFService/Dashboard.svc/GetCurrentReclaimedH2OMetric",
    dataType: "json",
    data: '{"currentDate": "' + jsonDate + '"}',
    success: function (results) {
        rawResults = results;
    },
    error: function (xhr) {
        alert(xhr.responseText);
    }
});

以下代码行尝试使用问题作为参考var jsonDate = '\\/Date(' + currentDate.getTime() + ')\\/';以正确的 JSON 格式格式化日期

4

2 回答 2

0

I modified the WCF service signature to accept a long data type instead of the System.DateTime object.

Also, I was having issues with how I was formatting the data property of the ajax jQuery call. Here is the updated code that worked:

var rawResults;
var currentDate = new Date('10/1/2012 12:00 AM');
var jsonDate = ConvertDateToTicks(currentDate);

$.ajax(
{
    async: false,
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://localhost/Dashboard_WaterResources/WCFService/Dashboard.svc/GetCurrentReclaimedH2OMetric",
    data: { dateInTicks: jsonDate },
    success: function (results) {
        rawResults = results;
    },
    error: function (xhr) {
        alert(xhr.responseText);
    }
});
于 2013-05-29T17:44:38.630 回答
0

jsonDate 中的整数应该是自纪元以来的毫秒数。

如何将 JavaScript 日期对象转换为刻度

或者考虑使用Moment.js来获取毫秒数。

于 2013-05-29T16:04:29.020 回答