List of JS objects in the C# MVC Controller does not appear in the Request.Cookies

Asked

Viewed 34 times

0

I’m putting the following Cookie:

document.cookie = "Teste=" + '[{"Id":20,"Nome":"Prato Feito","Valor":12.5,"Quantidade":1,"Imagem":"/images/ImgEmpty/noImg.jpg","Descricao":"Batata Palha, Cebola, Tomate"}]';

it appears in the list of Cookies in the browser: Cookie Teste

I can use the Cookie in JS to do whatever I want... But when I try to find it in the controller, it doesn’t appear in the Cookie list:

inserir a descrição da imagem aqui

What should I do? I realized if there are 'spaces' in the cookie happens same thing:

document.cookie = "Teste=Hello World"

but if you do:

document.cookie = "Teste=HelloWorld", já aparece ja lista dos cookies no controller.

1 answer

0

People could solve where I used to add cookie just put

encodeURIComponent

document.cookie = 'Nome=' + encodeURIComponent((JSON.stringify(produtos)));

and for reading

decodeURIComponent

I pick the cookie by name then use 'decodeURIComponent decode':

var cookieValue = document.cookie.replace(/(?:(?:^|.*;\s*)Nome\s*\=\s*([^;]*).*$)|^.*$/, "$1");
var dec = decodeURIComponent(cookieValue);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.