Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to encode value with jquery serialize?

I tried to encode all values like

encodeURIComponent($("#customer_details").serialize());

and that doesn't work as expected.

Is there way to get all elements on form and use encodeURIComponent to encode each value?

like image 203
Gihan Lasita Avatar asked Jul 11 '11 17:07

Gihan Lasita


People also ask

What is the use of serialize () method in jQuery?

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.

How do you serialize form data?

To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.

How do you serialize data in JavaScript?

In JavaScript, for example, you can serialize an object to a JSON string by calling the function JSON. stringify() . CSS values are serialized by calling the function CSSStyleDeclaration. getPropertyValue() .


1 Answers

It should already be encoded when using the serialize()[docs] method.

From the docs:

The .serialize() method creates a text string in standard URL-encoded notation.

Example: http://jsfiddle.net/WArUG/

If you want to represent a space with a %20 instead of a +, you'll need to do a .replace(/\+/g,'%20').

like image 167
user113716 Avatar answered Oct 08 '22 23:10

user113716