Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8858929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:56:18+00:00 2026-06-14T14:56:18+00:00

Can I set a session variable in jQuery? I need to store some data

  • 0

Can I set a session variable in jQuery?

I need to store some data in the session from jQuery and I also want to retrieve that data via jQuery.

Is there anything like

$.getSession.set("var","value");
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:56:19+00:00Added an answer on June 14, 2026 at 2:56 pm

    Session is accessible at the server side. But if you want, you can access your session by writing a function, which for example invokes ajax request to your server and gets whatever you want.

    EDIT:

    I’ll show you this using ASP.NET MVC framework, as I like it much. For the other frameworks idea is the same – just ask server about session.

    So, let it be like below at the server side – simple controller and 2 basic actions are written:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {            
            // store sample data
            Session["user"] = new { name = "Anton Chigurh", age = 42 };
            return View();
        }
    
        public JsonResult GetSessionValue(string key)
        {
            return Json(Session[key]);
        }        
    }
    

    Firstly, you have to store something into the session, for example by invoking Index action. Next, invoke the GetSessionValue action to retrieve value previously stored. Unless you have disabled session for your application, or cookies in your browser, you should get correct value.

    How to get the value ? Just send ajax request:

    1. You can write javascript function which will do synchronous request – which in fact can freeze your browser for some minor time (depends on your server time consumption used for some calculations, network infrastructure conditions etc.):

      function getSessionValue(key) {
          var result;
          $.ajax({
              url: "/Home/GetSessionValue",
              type: 'POST',
              dataType: 'json',
              data: JSON.stringify({ key: key }),
              contentType: 'application/json; charset=utf-8',
              async: false
          }).done(function (data) {
              result = data;
          });
          return result;
      }
      
      // invoke
      var user = getSessionValue("user");
      console.log(user.name + ": " + user.age);
      
    2. or an asynchronous version, which invokes a callback when done:

      function getSessionValue(key, callback) {    
          $.ajax({
              url: "/Home/GetSessionValue",
              type: 'POST',
              dataType: 'json',
              data: JSON.stringify({ key: key }),
              contentType: 'application/json; charset=utf-8',        
          }).done(function (data) {
              callback(data);
          });
      }
      
      // invoke 
      getSessionValue("user", function(x) {
          console.log(x.name + ": " + x.age);
      });
      

    BTW: I’ve used json seriazlization, as it’s the approach I prefer while passing the objects to and from the server. Other thing – jQuery done event will only be triggered on success. In case of some errors at the server side (ex: uncaught exception), request returns http 500 and event is not invoked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can i set a session from a smarty template page? I want to
How can I access session variable from the HttpModule ? I set following session
I understand that we can set the various style attributes from code behind using
I need to set a PHP $_SESSION variable using the jQuery. IF the user
As the title states, I have a session variable that is set during a
I'm currently learning Sinatra, and I can't seem to set a session variable to
I have secured pages that all check for a set session variable to determine
How can I can set a global variable for the username of the logged-in
In Photoshop you can set a layer's blending mode to be Hue. If that
In the layout you can set the EditText widget to be non-editable via the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.