Home > Back-end >  Storing and Retrieving Session variables in jQuery
Storing and Retrieving Session variables in jQuery

Time:11-09

I am trying to store some server info in a session variable and retrieve it another page and am running into a roadblock.

I get the information using an Ajax call and store the result in a session variable (I am using jquery.sessions library). The returned data is an array of objects.

function setServerInfo() { 
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        url: '/WebService/ABDC.asmx/GetServerInfo',
        cache: false,
        data: null,
    }).done(function (result) {debugger
        var jResult = JSON.parse(result.d);
        $.session.set('ServerIDs', jResult);
    }).fail(function (jqXHR, textStatus, errorThrown) {
    });
}

When I inspect jResult it contains an array of 12 objects of the form:

[0]

  APP_SVR_ID: 1
  SITE_ID: 123
  SVR_ID: 456
  SVR_IDs: 1,456

[1]
    ...
    ...

When I try to retrieve this data using

var ServerIDs = $.session.get('ServerIDs'); 

I get this (note the double quotes):

"[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]"

CodePudding user response:

You cant store a js object in storage directly, you have to stringify it most commonly by using JSON.stringify or don't parse it at all before you store it,otherwise you will get the types of results as you've received.

  • Related