How to get value in the session in jQuery

橙三吉。 提交于 2019-11-29 16:31:51

问题


I'm a beginner in jQuery.
I want to set the values in an HTML page and I have to get them in another HTML page.

Here is the piece of code I am trying now:

To set the value in session:

$.session.set(userName, $("#uname").val());

To get the value from session:

$.session.get('userName');

回答1:


Assuming you are using this plugin, you are misusing the .set method. .set must be passed the name of the key as a string as well as the value. I suppose you meant to write:

$.session.set("userName", $("#uname").val());

This sets the userName key in session storage to the value of the input, and allows you to retrieve it using:

$.session.get('userName');



回答2:


Sessions are stored on the server and are set from server side code, not client side code such as JavaScript.

What you want is a cookie, someone's given a brilliant explanation in this Stack Overflow question here: How do I set/unset cookie with jQuery?

You could potentially use sessions and set/retrieve them with jQuery and AJAX, but it's complete overkill if Cookies will do the trick.



来源:https://stackoverflow.com/questions/15510590/how-to-get-value-in-the-session-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!