This is a migrated thread and some comments may be shown as answers.

[Solved] Problems With jQuery 1.4, AJAX And Dates

1 Answer 95 Views
Grid
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Kevin Watkins
Top achievements
Rank 1
Kevin Watkins asked on 19 Jan 2010, 11:51 AM
Hi,

The current version of the grid has problems with Dates in AJAX requests when using jQuery 1.4. This is because jQuery 1.4 does strict JSON parsing so will throw a parsing error when it encounters "new Date" in the JSON.

To fix things, change your telerik.grid.js as follows:

1. Add the following after the line starting var dateRegExp at the top of the file:

var dateReviverRegExp = /\/Date\((.*?)\)\//g;  
 
var dateReviver = function(key, value)  
{  
    if (typeof (value) === "string")  
    {  
        var match = dateReviverRegExp.exec(value);  
        if (match)  
        {  
            return new Date(parseInt(match[1]));  
        }  
    }   
    return value;  

2. Change the options in ajaxRequest to the following:

var options = {  
    type: 'POST',  
    url: this.ajaxUrl,  
    dataType: 'text',  
    error: $t.bind(thisfunction(xhr, status)  
    {  
        if ($t.ajaxError(this.element, 'error.Grid', xhr, status))  
            return;  
 
        if (status == 'parsererror')  
            alert('Error! The requested URL did not return JSON.');  
    }),  
    complete: function()  
    {  
        clearTimeout(loadingIconTimeout);  
        statusIcon.removeClass('t-loading');  
    },  
    success: $t.bind(thisfunction(data)  
    {  
        // Parse the JSON manually so we can convert the dates to true Date objects.  
        if (window.JSON && window.JSON.parse)  
        {  
            data = window.JSON.parse(data, dateReviver);  
        }   
        else 
        {  
            data = (new Function("return " + data.replace(dateRegExp, 'new Date($1)')))();  
        }   
 
        data = data.d || data; //Support the `d` returned by MS Web Services   
        this.total = data.total || data.Total || 0;  
        this.dataBind(data.data || data.Data);  
    })  
}; 

Sadly jQuery 1.4 doesn't allow you to specify a reviver function to parse to the JSON.parse call (I've asked for that to be added to jQuery) which would make the patch simpler; this is why I'm specifying a type of text and parsing manually in the code above. Note that I'm not doing any regular expression checks to make sure this is valid JSON; you might want to add that in if you can't trust the JSON. (Have a look at the httpData method in jQuery for how to do this)

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 19 Jan 2010, 12:00 PM
Hello Kevin Watkins,

We are aware of this problem and have fixed it internally. We plan to release a service pack by the end of this week. It will address jquery 1.4 compatibility issues as well as a few other problems.

Regards,
Atanas Korchev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Tags
Grid
Asked by
Kevin Watkins
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or