Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Aaron Selonke
Aaron Selonke
10,323 Points

When is it necessary to .serialize() in an AJAX post request; ?

When using jQuery to make AJAX POST requests, Is it always necessary to format the data with .serialize() before sending up to the server?

Is it always necessary when sending data from HTML forms?

If yes, what kind of AJAX post requests would not need the .serialize() method?

1 Answer

Allison Hanna
Allison Hanna
36,222 Points

Use serialize when you need to encode a set of form elements as a string for submission. https://api.jquery.com/serialize/

"The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request."

So you would use it when you are selecting form elements (like check boxes, radio buttons, input strings) and you need to put them together to be part of a query on a URL.

Aaron Selonke
Aaron Selonke
10,323 Points

So do GET and POST data collected from forms elements both need to be formatted with serialize before being send to the server?
Only GET requests send data up on the URL query string. correct?