0

I have a method which sends an ajax request. When the reply from the server is received I need to serialize and later de-serialize

$.ajax({
//.....
done(function(data) {

//1 Need to serialize data (which is an array)


});

function myFunction() {
  //2 Need to de-serialize data which has been serialized

}

I know I could use jquery#serializeArray() if I had a form to serialize:

$( "form" ).submit(function( event ) {
  console.log( $( this ).serializeArray() );
  event.preventDefault();
});

But I don't have a form and data from the server (I guess) has nothing to do with serializeArray function of jquery. So how can I do it? What's one of the best ways?

Preferably not to use any third-party libraries except jquery or even not to use jquery at all.

2
  • I guess reading $.ajax documentation helps. Commented Jun 24, 2014 at 10:17
  • @Teemu, not really. Do you mean I can just write "data" to an html page as it is? Commented Jun 24, 2014 at 10:26

1 Answer 1

1

The common way to serialize JS-objects to JSON is via JSON.stringify().

The other way around is via JSON.parse().

o={"firstName":"john","lastName":"doe"};

console.log(JSON.stringify(o));
console.log(JSON.parse(JSON.stringify(o)));   

See MDN for stringify and parse

Here is a Fiddle.

.serializeArray() from jQuery is only a neat helper function to serialize form-data. It builds its objects from the ground up. Here is the source for that. If you want to submit your data as JSON, you simply

$.ajax({
  type: "POST",
  url: url,
  data: JSON.stringify(data),
  success: success,
  dataType: dataType
});   

Free after jQuery.post().

Sign up to request clarification or add additional context in comments.

4 Comments

where should I serialize it to (when I've received data inside "done" callback): into a div or some js variable?
The need for serialization is, to get JS-objects through the wire. so, if you want to POST it to the server, you serialize it. If you are receiving it from the server, it is already serialized - assuming, you used jQuery to do the job. You only have to use the result. When you receive the data, it depends, on what you are going to do with it.
I receive it and I want to access the result from outside of .done(...) callback, in other words I want to pass the result around.
The problem you are facing is that of a closure: you are passing a function into jQuery on whose return value, you have no access. There are several possible solutions to that: 1) callback. Within the closure you call a function from "outside" of the closure with "data" as a parameter 2) you could assign it to a variable outside of the closure, but have the problem, of not knowing when the variable is set. To circumvent that, you could make use of an eventmechanism.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.