Skip to content

Use decodeURIComponent to decode cookie before parsing it #245

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 9, 2015

Conversation

skonsoft
Copy link
Contributor

@skonsoft skonsoft commented Nov 7, 2015

When we use this function as it is, we get a javascript error saying:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
    var a=JSON.parse(b);console.log(a);...

In the server side, json_encode encrypt the cookie in HTTP, so to parse the cookie we should decrypt that can by using decodeURIComponent javascript function.

When we use this function as it is, we get a javascript error saying:

```
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
	var a=JSON.parse(b);console.log(a);...
```
In the server side, json_encode encrypt the cookie in HTTP, so to parse the cookie we should decrypt that can by using decodeURIComponent javascript function.
@dbu
Copy link
Contributor

dbu commented Nov 9, 2015

oh, did you get the flash message handler to work? that sounds great!

if you could read over the documentation section and clarify it, that would be great. we ported it from the old LiipCacheControlBundle but found nobody actually using it... i am not even 100% sure i understand the idea: we respond with a cookie during the redirect message after a POST that can't be cached anyways? so that the GET request after the redirection can be cached, and a flash message displayed through javascript? if i have that correctly, we should update the documentation to explain this process.

dbu added a commit that referenced this pull request Nov 9, 2015
Use decodeURIComponent to decode cookie before parsing it
@dbu dbu merged commit 14580e8 into FriendsOfSymfony:master Nov 9, 2015
@skonsoft
Copy link
Contributor Author

skonsoft commented Nov 9, 2015

I don't know what the problem exactly, but here the code that i used to show flashes using the javascript and cookies:

var flashDivId = '#flashes',
        flashCookieName = 'flashes'; // fos_http_cache.flash_message.name

function getCookie(cname)
{
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }

    return false;
}

function showFlash()
{
    var cookie = getCookie(flashCookieName);

    if (!cookie) {
        return;
    }

    cookie = cookie.replace(/\+/g, '%20'); //replace the + by an http encoded element
    var flashes = jQuery.parseJSON(decodeURIComponent(cookie));

    var html = '';

    for (var key in flashes) {
        if (key === 'length' || !flashes.hasOwnProperty(key)) {
            continue;
        }
        var msg = flashes[key];
        html = '<div class="alert alert-' + key + '">';
        html += '<i class="fa fa-info-circle"></i>';
        html += msg;
        html += '</div>';
    }

// show flashes in your DOM...
    $(flashDivId).html(html);

    //Remove the cookie
    $.removeCookie(flashCookieName, {path: '/'});
}

// register showFlash on the page ready event.

$(function () {
    showFlash();
});

I hope this help you to fix this issue.

@dbu
Copy link
Contributor

dbu commented Nov 10, 2015

thanks. added some of this in #248

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants