I wish to serialize some session content for an Ajax request. Something similar to this:
var data = $.session("data").serialize();
It is for a standard jQuery ajax function like this:
$.ajax({
type: "POST",
url: "script.php",
data: data,
success: function() {
...
}
});
The serialize() only operates on form elements, so I am looking for something similar. Something that handles an array in a form representing a SESSION data array.
Does that exist or do I have to use a foreach loop to serialize the session content in a POST-friendly string manually?
The main real reason to use the serialize function on data like that is when it originates from several sources – such as a form element containing many input fields. One serialize command on the form will return all the data.
If you already have some JavaSctipt variables holding your “session” data, you can simply send them as an object through the AJAX call – there is no need to serialize the data if you already have all of it in one variable.
If you are wanting to pass some server session variables then you might want to consider the following approach –
You can save your session variable data and encode it to a json object using
json_encode(). After that you can pass it to your JavaScript simply by echoing it out into a variable –Then you can just pass the variable with your AJAX call – no need to serialize the data.