greenbandit
greenbandit

Reputation: 2275

jQuery access to serialized data

I need to pass some variables from a cookie to jQuery, but I got this result:

a:2:{s:4:"wait";s:3:"600";s:2:"ip";i:168427521;}

Which is serialized, how I can access to these variables from jQuery?

Edit: I've updated my code: now I got this:

console.log(cookie);

and prints:

{"wait":"600","ip":168427521}

how I can access those variables?

Upvotes: 2

Views: 1072

Answers (2)

blockhead
blockhead

Reputation: 9705

var obj = jQuery.parseJSON(cookie);
console.log(obj.wait,obj.ip);`

Upvotes: 1

Tommaso Barbugli
Tommaso Barbugli

Reputation: 12031

I guess you need to deserialize php serialized vars using javascript, there's nothing like that in jquery (not a builtin at least)

You've got several options here:

1- deserialize with php and pass that to js as json

2- change serialization and use json (json serialization is builtin in jQuery) also for the cookie

3- use something like this to deserialize php objects to javascript -> http://phpjs.org/functions/unserialize:571 so that you can do something like in js

unserialize('a:2:{s:4:"wait";s:3:"600";s:2:"ip";i:168427521;}');

Upvotes: 3

Related Questions