最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Angular cookies appearing with encoded characters - Stack Overflow

programmeradmin3浏览0评论

Given some data:

$scope.devices = [
  { name: 'iPhone 4', os: 'iOS'},
  { name: 'Nexus 7', os: 'Android'},
];

And a function to set a cookie:

$scope.saveDeviceChoice = function() {
  $cookieStore.put('savedDevice', $scope.deviceChoice.name);
}

I get a cookie with value %22Nexus%207%22 instead of what I would expect: Nexus 7.

This is what it looks like in the web inspector

I am truly puzzled - it seems that %22 is a quote and %20 is a space, somehow the value gets saved "encoded". Who knows what is going on?

Given some data:

$scope.devices = [
  { name: 'iPhone 4', os: 'iOS'},
  { name: 'Nexus 7', os: 'Android'},
];

And a function to set a cookie:

$scope.saveDeviceChoice = function() {
  $cookieStore.put('savedDevice', $scope.deviceChoice.name);
}

I get a cookie with value %22Nexus%207%22 instead of what I would expect: Nexus 7.

This is what it looks like in the web inspector

I am truly puzzled - it seems that %22 is a quote and %20 is a space, somehow the value gets saved "encoded". Who knows what is going on?

Share Improve this question edited Oct 6, 2018 at 19:31 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Jun 10, 2013 at 15:57 WolfrWolfr 5,1644 gold badges27 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

Here is the official doc for $cookieStore:

Provides a key-value (string-object) storage, that is backed by session cookies. Objects put or retrieved from this storage are automatically serialized or deserialized by angular's toJson/fromJson.

Then the store save the URL encoded version of the value. Take a look at this article, there is a section explaining the cookie encoding.

$cookieStore.(get/put) automatically run to/from Json on the value you send over, which appends the encoded characters.

If you were to just use $cookies then you can get away with setting your parameters as such

$cookies['savedDevice'] = $scope.deviceChoice.name;

or

$cookies.savedDevice = $scope.deviceChoice.name;

Either way, the values are saved just like you want em to be.

Usage of $cookieStore is popular, however es with it's own baggage if you want to use the values on the server.

发布评论

评论列表(0)

  1. 暂无评论