user3172663
user3172663

Reputation: 312

Unable to set cookie in java script

Trying to set cookie in js using stringify() with added lib json2.js

var data = {
   fields: fields,
   rows: rows,
   data: rows
};

Tried like below :

$.cookie("instant_view", JSON.stringify(data)); //instant_view as cookie name

but could not get any cookie (trying find the cookie name through firefox view cookie info)

also tried for

var json_str = JSON.stringify(data);
$.cookie('mycookie', json_str);

Still, unable find or set the cookie name.

Upvotes: 0

Views: 555

Answers (3)

user3172663
user3172663

Reputation: 312

Thanks all for your answers.. cookies is set httpOnly which is not compatibility for js and i got how to solve the issue .

Upvotes: 0

Rohan Kumar
Rohan Kumar

Reputation: 40639

Did you add $.cookie plugin before using it?

You will get your cookie data by using

var cookie_data= $.cookie('mycookie');
console.log(cookie_data);

Also you need to be careful about your json-data it should not be more than 4KB Read What is the maximum size of a web browser's cookie's key?

Cookie Demo

Updated, you can use webstorage for storing data more than 4KB Read Wiki Web sorage and What is the max size of localStorage values?

Upvotes: 0

Cyassin
Cyassin

Reputation: 1490

Make sure your cookie does not have the httpOnly flag set, as you will not be able to manipulate it with javascript.

Check your cookies flags for httpOnly.

Upvotes: 1

Related Questions