0

I have the next snippet of javascript code:

wdata['account'] = {"value": $(input).val(), "title": "Номер карты получения"};
var r = {"ipayway": ipw_selected,
        "wpayway": wpw_selected,
        "amount_type": amount_type,
        "amount": amount,
        "email": email,
        "idata": idata,
        "wdata": wdata,
        "udata": udata
      }
console.log(JSON.stringify(r));
document.cookie = "r=" + JSON.stringify(r) + ";";
console.log(document.cookie);

Debug results in chrome and firefox:

{"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"refer@faster.com","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"Номер карты получения"}},"udata":{}}

_ga=GA1.2.1726207989.1464355649; _ym_uid=1464355650833173718; _ym_isad=2; r%3Dfalse%3B; r={"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"refer@faster.com","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"Номер карты получения"}},"udata":{}}; _gat=1; _ym_visorc_37606275=w

In safari:

{"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"refer@faster.com","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"Номер карты получения"}},"udata":{}}


r={"ipayway":"3","wpayway":"2","amount_type":"invoice","amount":"10","email":"refer@faster.com","idata":{"phone":{"value":"79111111111","title":"phone"}},"wdata":{"account":{"value":"4444444444444448","title":"A1468837743323372246t%3A1468837754%3A; ga=GA1.2.1861993315.1468837742; gat=1; ym_isad=2; ym_uid=1468837743323372246; _ym_visorc_37606275=w

I don't know what happens. Why instead "title":"Номер карты получения" like in chrome or firefox I got this trash in safari "title":"A1468837743323372246t%3A1468837754%3A. Also indeed safari cutts off the last closed braces if you compare braces in debug results.

discort
  • 678
  • 1
  • 8
  • 26
  • What happens if you try to set the cookie to a simple string with the same "Номер карты получения" text as is going wrong within the JSON? Or if you store JSON with a title that uses standard English letters? – nnnnnn Jul 18 '16 at 11:28
  • `document.cookie = 'r="Номер карты получения"';` `"_ym_isad=2; _ym_uid=1468841469115532439; r=\"z"` – discort Jul 18 '16 at 11:37
  • @nnnnnn if you see debug results JSON.stringify produces correct result. But whet I try to write string with non-ascii symbols to cookie only trash saves – discort Jul 18 '16 at 11:41

2 Answers2

0

You need to add "charset=utf-8" in HTML Meta Tag, and javascript.

<meta charset="utf-8">

And

<script type="text/javascript" charset="utf-8" src="/js/xxx.js"></script>
Joey Etamity
  • 856
  • 4
  • 9
0

I found the very useful information at https://stackoverflow.com/a/1969339/3960038 Safari simply refuses to send any cookie containing non-ASCII characters that's why for cross-browsing need to use encodeURIComponent

In my case it will be:

wdata['account'] = {"value": $(input).val(), "title": encodeURIComponent("Номер карты получения")};
var r = {"ipayway": ipw_selected,
    "wpayway": wpw_selected,
    "amount_type": amount_type,
    "amount": amount,
    "email": email,
    "idata": idata,
    "wdata": wdata,
    "udata": udata
  }
document.cookie = "r=" + JSON.stringify(r) + ";

Cookie will be contain the title value: %D0%9D%D0%BE%D0%BC%D0%B5%D1%80%20%D0%BA%D0%B0%D1%80%D1%82%D1%8B%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%BD%D0%B8%D1%8F which can be easy handle on Back-End. For example in python:

import urllib
title = '%D0%9D%D0%BE%D0%BC%D0%B5%D1%80%20%D0%BA%D0%B0%D1%80%D1%82%D1%8B%20%D0%BF%D0%BE%D0%BB%D1%83%D1%87%D0%B5%D0%BD%D0%B8%D1%8F'
>>> print urllib.unquote(a)
Номер карты получения
Community
  • 1
  • 1
discort
  • 678
  • 1
  • 8
  • 26