Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?