- Ext JS 3.0 Cookbook
- Jorge Ramon
- 329字
- 2021-04-01 13:43:44
Encoding and decoding URL data
Two-way conversion between objects and URL data is a challenge that Ext JS can help with. Let's examine how a JavaScript object can be encoded for transmission through the URL query string, as well as how information contained in a URL can be used to build a JavaScript object.
How to do it...
The following steps will guide you through the process of encoding and decoding URL data:
- Take a
selectedColors
object as the data to be passed in a URL:var selectedColors = {color1:'Blue', color2:'Red', color3:'White'};
- Convert the object to URL data like this:
var encodedUrl = Ext.urlEncode(selectedColors); // encodedUrl is an encoded URL query string: //color1=Blue&color2=Red&color3=White.
- Now, a URL can be built using the data just created. For example,
http://MyGreatApp/SetSelectedColors?color1=Blue&color2=Red&color3=White.
- You can easily create objects from the encoded URL. Assuming we obtained the data from the URL we used above
(http://MyGreatApp/SetSelectedColors?color1=Blue&color2=Red&color3=White)
, obtain the URL data like this:encodedUrl = location.search;
- Re-create the
selectedColors
object as follows:var selectedColors = Ext.urlDecode(encodedUrl); // Now the value of selectedColors' color1 property is 'Blue', // color2's value is 'Red' and color3's value is 'White'.
Ext.urlEncode(object)
and Ext.urlDecode(string, overwrite)
provide object serialization to URL data and URL data deserialization to objects respectively. Encoding is accomplished by creating the URL query string's key-value pairs based on each object property, or array value passed to the encoding function. Decoding is accomplished by creating an object with a property for each key-value pair that exists in the URL's query string.
You can use this recipe when your application needs to send information to the server via AJAX or standard HTTP requests, as well as when you need to use the URL's query string to feed the application data that can later be converted to JavaScript objects.
- 3ds Max 2014標(biāo)準(zhǔn)教程(全視頻微課版)
- AutoCAD 2014中文版完全自學(xué)手冊
- DWR Java AJAX Applications
- Maya 2019三維動畫基礎(chǔ)案例教程
- Plone 3 Multimedia
- NHibernate 2 Beginner's Guide
- iPad+Procreate室內(nèi)設(shè)計手繪表現(xiàn)技法
- SolidWorks 2018快速入門及應(yīng)用技巧
- Liferay Portal Systems Development
- Practical Data Analysis and Reporting with BIRT
- 中文版Photoshop CC基礎(chǔ)教程
- 中文版3ds Max 2016/VRay效果圖制作實(shí)戰(zhàn)基礎(chǔ)教程(全彩版)
- 3ds Max 印象 影視粒子特效全解析
- 剪映專業(yè)版:短視頻創(chuàng)作案例教程(全彩慕課版)
- Inkscape 0.48 Essentials for Web Designers