Serializar el contenido de la sesión para Ajax

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 funciona así:

$.ajax({
   type: "POST",
   url: "script.php",
   data: data,
   success: function() {
     ...
   }
});

Las publicar por fascículos() 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?

preguntado el 28 de julio de 12 a las 21:07

¿Qué $.session("data") refer to? Are you using a plugin and browser-side sessionStorage? Si es jQuery-Session-Plugin, I don't know that $.session().serialize() will do anything, unless $.session('data') was itself an object/array to serialize as it is stored. -

2 Respuestas

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 -

var sess_data = '<?php echo json_encode($_SESSION); ?>' ;

Then you can just pass the variable with your AJAX call - no need to serialize the data.

$.ajax({
   type: "POST",
   url: "script.php",
   data: sess_data,
   success: function() {
     ...
   }
});

Respondido 28 Jul 12, 21:07

I think the OP is using the jQuery Session Plugin. - Jared Farrish

It's a little unclear to me if it is an array or an object or whatever, but you can probably use $ .param para esto.

De los documentos:

Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.

--

var data = $.session("data");

$.ajax({
   type: "POST",
   url: "script.php",
   data: {data: $.param(data)},
   success: function() {
     ...
   }
});

and catch it in PHP

parse_str($_POST['data'], $myVariable);

Respondido 28 Jul 12, 21:07

"Encode a set of form elements as an array of names and values." My session data is not form elements though. - Steeven

What is the improvement with using this over $.serialize() in this situation? How is another question an answer? - Jared Farrish

Look a little closer and you'll see $.session(), which I believe is a plugin working with sessionStorage. I've asked in a comment below the question for clarification. - Jared Farrish

@JaredFarrish - I don't really the know the plugin, so I have no idea what that function outputs, it could be anything? The question is'nt really clear on what exactly it is either. Edited my answer, and I believe it will work either way? - adeneo

Well $.param should work with both an array or an object, so unless it's something else completely, that should work ? - adeneo

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.