【问题标题】:$.ajax parsererror when handling JSON containing a new Date() element处理包含新 Date() 元素的 JSON 时的 $.ajax 解析器错误
【发布时间】:2013-09-11 17:41:56
【问题描述】:

我正在序列化从 MongoDB 抓取的 JSON,并将其返回给通过 $.ajax 调用请求它的 javascript 客户端。

$.ajax 调用如下所示:

function server_request(URL, httpMethod, dataObject) {

    var output = "";
    var typeofData = "json";
    var dataToSend;

    console.log('server_request ajax [' + httpMethod + '] request to ' + URL + ' with ' + JSON.stringify(dataObject));

    if (dataObject !== null) {
        dataToSend = JSON.stringify(dataObject);

        $.ajax({
            type: httpMethod,
            async: false,
            dataType: typeofData,
            data: { jsonPost: dataToSend },
            timeout: 2000, // Timeout in milliseconds
            url: URL,
            success: function (data) {
                output = data;
            }
            , error: function (jqXHR, textStatus, errorThrown) {
                console.log('Ajax post error jqXHR: ' + JSON.stringify(jqXHR));
                console.log('Ajax post error: ' + textStatus);

                output = new Object();
                output.textStatus = textStatus;
                output.errorThrown = errorThrown;
                output.nutshell = 'n/a - exception thrown';

                console.log('Ajax post error: ' + JSON.stringify(output));
            }

        });
    }

在服务器上,我从 MVC 控制器返回 JSON,如下所示:

return new ContentResult { Content = responseDoc.ToJson(), ContentType = "application/json" };

其中 responseDoc 是 MongoDB BsonDocument。

只要我在 JSON 中没有日期元素,一切都可以正常工作。一旦添加了如下所示的元素:\"usersynctimestamp\" : new Date(1378905216401),$.ajax 调用就会失败并出现解析器错误。

当它失败时,我仍然可以通过错误XHR数据看到有问题的JSON,如下:

Ajax post error jqXHR: {"readyState":4,"responseText":"{ \"nutshell\" : \"yes\", \"mongoDocument\" : { \"_id\" : { \"$binary\" : \"TJop8k0+KUKfrEVzI/vV+A==\", \"$type\" : \"03\" }, \"companyname\" : \"Apple\", \"databasename\" : \"Apple\", \"users\" : [{ \"_id\" : { \"$binary\" : \"y2dgrURxUEq/iCdI/eIoPw==\", \"$type\" : \"03\" }, \"email\" : \"marketpoint@evoite.com\", \"firstname\" : \"Ian\", \"password\" : \"aa\", \"surname\" : \"Smith\" }], \"usersynctimestamp\" : new Date(1378905216401) }, \"mongoDocumentCollectionName\" : \"clients\" }","status":200,"statusText":"OK"} 

因此,$.ajax 似乎不喜欢由 MongoDB C# 驱动程序中的序列化程序创建的新 Date() 语法。谁能告诉我如何解决这个问题?

非常感谢

【问题讨论】:

  • JSON 没有日期序列化/反序列化的概念。您必须使用像 moment.js 这样的工具来为您处理这些事情或自己实现逻辑。

标签: javascript jquery ajax json mongodb


【解决方案1】:

你需要强制MongoDB驱动输出严格的json,如下:

string jsonStrictString = responseDoc.ToJson(new MongoDB.Bson.IO.JsonWriterSettings() 
                {
                    GuidRepresentation = GuidRepresentation.CSharpLegacy, 
                    Indent = false, 
                    OutputMode = MongoDB.Bson.IO.JsonOutputMode.Strict
                });

这将允许 $.ajax 正确解析响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-11
    • 2012-12-13
    • 2015-03-12
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多