Skip to main content
Source Link

I'm using jQuery with the cookies library that automatically JSON-stringifies structures when setting cookies and rebuilds them again when getting the cookie.

I'm using cookies to provide an undo-redo functionality for a web form that can include a lot of text, so every time an input element changes I collect all the data in the form and put it all in a cookie called newsletterDoX (where X is an integer) and I also set another cookie to store the current integer and another to store how many redos are available. Cookies are set to expire after one day.

The functionality is working, but when there is more than 4 newsletterDo cookies then a page refresh will give me a 400 error. In Chome it says

Bad Request

Your browser sent a request that this server could not understand. Size of a request header field exceeds server limit.

Cookie

/n

When I delete the cookies back down to 4 or less then the error goes away. I think I got to 5 or 6 without the error with Firefox.

What's going on? I don't think it's the size of the cookies, or the content. The cookies beyond 4 can have the same size and content as those below 4 and it still gives the error.

The total number of cookies in the domain is as low as 12 when the error occurs, and deleting some other cookies rather than the newsletterDo cookies doesn't seem to change things, so the number of cookies probably isn't a problem either.

What could it be? I'm stumped!

here's the code that sets the cookies:

// cookies expire 1 day from now.
// I'm sure there's a more succinct way to set the expiry date,
// but this is working for me
var oneDay = new Date();
oneDay.setDate(oneDay.getDate() + 1);
jQuery.cookies.setOptions({expiresAt: oneDay});

// this function saves the form data so the user can come back to it
var setNewsletterCookie = function() {
    var saveData = collectNewsletterData();
    // save it in the database using the php code that acts as a db interface
    jQuery.post('db_interface_newsletters.php', {
        task: "save",
        title: $('#newsletterTitle').val(),
        issue: $('#issuenum').val(),
        content: saveData},
        function(data) {
            if (data.indexOf('Fail') >= 0) {alert (data);}
            else {
                // data should be a date string in UTC
                // convert it to local time and display
                var lastSaveDate = new Date(data);
                $('.last_save_date').text(lastSaveDate.toLocaleString());
            }
        }
    );
    // save it also in a cookie for use of undo and redo
    try {
        var currentNewsletterDo = parseInt(jQuery.cookies.get('current_newsletter_do'), 10);
        if (isNaN(currentNewsletterDo)) currentNewsletterDo = 0;
    } catch(e) {
        var currentNewsletterDo = 0;
    }
    ++currentNewsletterDo;
    jQuery.cookies.set('newsletterDo' + currentNewsletterDo, saveData);
    jQuery.cookies.set('current_newsletter_do', currentNewsletterDo);
    jQuery.cookies.set('newsletter_available_redo', 0);
    $('.newsletter_redo').button('disable');
    $('.newsletter_undo').button('enable');
};
Post Made Community Wiki by Toby 1 Kenobi