Both sides previous revision
Previous revision
Next revision
|
Previous revision
|
programming:collection.next_json [2016/11/02 16:18] jlucas |
programming:collection.next_json [2016/11/02 17:04] (current) jlucas |
</p> | </p> |
| |
<pre><div style="text-align:left;color:#000000; font-size:1em; font-family:monospace; "><em><span style="color:#236e25;">/** | <pre> |
* encodeURIComponent() function is not 100% compatible with | /** |
* RFC3986 http://www.faqs.org/rfcs/rfc3986.html | * encodeURIComponent() function is not 100% compatible with |
*/</span></em> | * RFC3986 http://www.faqs.org/rfcs/rfc3986.html |
<span style="color:#881350;">function</span> <span style="color:#003369;">encodeRFC3986</span>(value) { | */ |
<span style="color:#881350;">return</span> <span style="color:#003369;">encodeURIComponent</span>(value) | function</span> encodeRFC3986</span>(value) { |
.<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/!/g</span>, <span style="color:#760f15;">"%21"</span>) | return</span> encodeURIComponent</span>(value) |
.<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\*</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">"%2A"</span>) | . replace</span>( /!/g</span>, "%21"</span>) |
.<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\(</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">"%28"</span>) | . replace</span>( /</span> \*</span> /g</span>, "%2A"</span>) |
.<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/</span><span style="color:#be73fd;">\)</span><span style="color:#661aa9;">/g</span>, <span style="color:#760f15;">"%29"</span>) | . replace</span>( /</span> \(</span> /g</span>, "%28"</span>) |
.<span style="color:#003369;">replace</span>(<span style="color:#661aa9;">/'/g</span>, <span style="color:#760f15;">"%27"</span>); | . replace</span>( /</span> \)</span> /g</span>, "%29"</span>) |
| . replace</span>( /'/g</span>, "%27"</span>); |
} | } |
| |
<span style="color:#881350;">function</span> <span style="color:#003369;">translateToFormUrlEncoded</span>(data) { | function</span> translateToFormUrlEncoded</span>(data) { |
<span style="color:#881350;">var</span> buffer = []; | var</span> buffer = []; |
| |
<span style="color:#881350;">for</span><span style="color:#003369;"> </span>(<span style="color:#881350;">var</span> i = <span style="color:#0000ff;">0</span>; i < data.length; i++) { | for</span> </span>( var</span> i = 0</span>; i < data.length; i++) { |
<span style="color:#881350;">var</span> input = data[i]; | var</span> input = data[i]; |
<span style="color:#881350;">var</span> name = input.name; | var</span> name = input.name; |
<span style="color:#881350;">var</span> value = input.value; | var</span> value = input.value; |
| |
<span style="color:#881350;">if</span><span style="color:#003369;"> </span>(value === null) { <em><span style="color:#236e25;">// convert null value to empty string | if</span> </span>(value === null) { <em> // convert null value to empty string |
</span></em> value = <span style="color:#760f15;">''</span>; | </span></em> value = ''</span>; |
} <span style="color:#881350;">else</span> <span style="color:#881350;">if</span><span style="color:#003369;"> </span>(<span style="color:#881350;">typeof</span> value === <span style="color:#760f15;">"boolean"</span>) { <em><span style="color:#236e25;">// convert boolean value to integer 0 or 1 | } else</span> if</span> </span>( typeof</span> value === "boolean"</span>) { <em> // convert boolean value to integer 0 or 1 |
</span></em> value = <span style="color:#003369;">Number</span>(value); | </span></em> value = Number</span>(value); |
} | } |
| |
buffer.<span style="color:#003369;">push</span>(<span style="color:#003369;">encodeRFC3986</span>(name) + <span style="color:#760f15;">"="</span> + <span style="color:#003369;">encodeRFC3986</span>(value)); | buffer. push</span>( encodeRFC3986</span>(name) + "="</span> + encodeRFC3986</span>(value)); |
} | } |
| |
<span style="color:#881350;">return</span> buffer.<span style="color:#003369;">join</span>(<span style="color:#760f15;">"&"</span>); | return</span> buffer. join</span>( "&"</span>); |
} | } |
| |
<span style="color:#881350;">var</span> data = [ | var</span> data = [ |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"first-name"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"John"</span>}, | { "name"</span>: "first-name"</span>, "value"</span>: "John"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"last-name"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"Doe"</span>}, | { "name"</span>: "last-name"</span>, "value"</span>: "Doe"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"email"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"john@doe.com"</span>}, | { "name"</span>: "email"</span>, "value"</span>: "john@doe.com"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"website"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"http://john.doe.com"</span>}, | { "name"</span>: "website"</span>, "value"</span>: "http://john.doe.com"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"age"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#0000ff;">37</span>}, | { "name"</span>: "age"</span>, "value"</span>: 37</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"interests"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"music"</span>}, | { "name"</span>: "interests"</span>, "value"</span>: "music"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"interests"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"sports"</span>}, | { "name"</span>: "interests"</span>, "value"</span>: "sports"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"interests"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#760f15;">"cars"</span>}, | { "name"</span>: "interests"</span>, "value"</span>: "cars"</span>}, |
{<span style="color:#760f15;">"name"</span>: <span style="color:#760f15;">"subscribe"</span>, <span style="color:#760f15;">"value"</span>: <span style="color:#881350;">false</span>} | { "name"</span>: "subscribe"</span>, "value"</span>: false</span>} |
]; | ]; |
| |
<em><span style="color:#236e25;">// convert data array to application/x-www-form-urlencoded | <em> // convert data array to application/x-www-form-urlencoded |
</span></em><span style="color:#881350;">var</span> postData = <span style="color:#003369;">translateToFormUrlEncoded</span>(data); | </span></em> var</span> postData = translateToFormUrlEncoded</span>(data); |
</div></div> | |
</pre> | </pre> |
| |
<pre><span class="comment">// encoded contents of resulting postData variable</span> | <pre> |
<code> | // encoded contents of resulting postData variable |
first-name=John&last-name=Doe&email=john%40doe.com&website=http%3A%2F%2Fjohn.doe.com&age=37&interests=music&interests=sports&interests=cars&subscribe=0 | first-name=John&last-name=Doe&email=john%40doe.com&website=http%3A%2F%2Fjohn.doe.com&age=37&interests=music&interests=sports&interests=cars&subscribe=0</pre> |
</code></pre> | |
| |
</section><!-- /translating to form urlencoded --> | </section><!-- /translating to form urlencoded --> |