SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 170 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"


When JSON data is returned from the server, it needs to be evaluated just like any other
data. Because of the potential for code injection attacks (never assume that the data you
receive is safe), I highly recommend using a client-side parser such as the one on the JSON
web site.
var obj = transport.responseText.parseJSON();
if(obj) performMagic(obj);
You would then check that the data you were expecting exists and then move on to
process the data accordingly.
CHAPTER 5 n AJAX AND DATA EXCHANGE 115
Delimited Strings
A delimited string is one that separates values with a character. For example, a query string
of a URL is separated by ampersands, and key/value pairs are separated by an equal sign:
search=my+search+phrase&sortBy=title&page=2
Converting a query string into a JavaScript object requires two passes to build up the
object. The first pass splits the string at the ampersand, and the second pass separates
the key/value pairs:
var qs = "search=my+search+phrase&sortBy=title&page=2";
var data = qs.split('&');
for(var i=0;i{
data[i] = data[i].split('=');
}
alert(data[1][1]); // alerts "title"
By the end, you??™d have a multidimensional array. To access a key/value pair, you??™d have
to know where in the array the value exists or else loop through all the items in the first
dimension of the array to find what you??™re looking for. Alternatively, you might want to turn
the key/value pairs into an object, making it easier to access the parameters:
var qsObject = {}; // our object store
var qs = "search=my+search+phrase&sortBy=title&page=2";
var data = qs.


Pages:
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
hotel jelenia góra Russian bride Free English grammar and study guid powiekszenia wielkoformatowe counter strike 1.6