JSON: Difference between revisions

From Medien Wiki
mNo edit summary
mNo edit summary
Line 228: Line 228:
===Serialize a form with [[JavaScript]]===
===Serialize a form with [[JavaScript]]===
<p>This is the part, where i want to show a few more things about JSON which is the JSON Parsers. JSON usually travels between web app and the server as a string, javascript has a really nice (and dangerous!) function which is <em><strong>eval()</strong></em> this function reads string values and interprets it as javascript, which is quite convenient if the JSON file comes from a secure source, however we can't assure the security of an external service like twitter, so i'm going to use a parser to convert the values of our form into a JSON, while i'm also going to show the opposite, convert a JSON into a Javascript Object with the parser</p>
<p>This is the part, where i want to show a few more things about JSON which is the JSON Parsers. JSON usually travels between web app and the server as a string, javascript has a really nice (and dangerous!) function which is <em><strong>eval()</strong></em> this function reads string values and interprets it as javascript, which is quite convenient if the JSON file comes from a secure source, however we can't assure the security of an external service like twitter, so i'm going to use a parser to convert the values of our form into a JSON, while i'm also going to show the opposite, convert a JSON into a Javascript Object with the parser</p>
<sub>To save a few lines i'll be using the [http://www.jquery.com jQuery Framework]</sub>
<source lang="javascript">
<form action="" method="get" accept-charset="utf-8">
<input type="text" name="username" value="username" id="tweet">
<input type="text" name="password" value="password" id="tweet">
<textarea name="tweet"></textarea>
<input type="submit" value="Send"></p>
</form>
<script type="text/javascript" src="jsonparser.js">
<script type="text/javascript">
$(document).ready(function(){
$('form').submit(function(e)){
e.preventDefault();
}
})
</script>
</source>