0 Replies - 513 Views - Last Post: 03 September 2011 - 12:37 AM

#1 cupidvogel  Icon User is offline

  • D.I.C Addict

Reputation: 31
  • View blog
  • Posts: 593
  • Joined: 25-November 10

getJSON() not working!

Posted 03 September 2011 - 12:37 AM

Hi, I wrote a quite complicated code to achieve something even more complicated, but I found it hard to debug, so I began cutting out portions of it, until I came down to the following snippet, which I can no find no reason for not working:

<html>
  <head>
    <title>Google It Google  Web Search API Sample</title>
	<script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
	$(function() {
	$("button#first").click(function() {
	 tex ="";
	 filename = $("select#cool option:selected").text();
         $.getJSON("filename", function(data) {
         $.each(data, function(index, entry) {
         tex+=entry['a'];
	 });
	 });
         $("div").text(tex);
	});
	});
	</script>
	</head>
	<body>
	 <select id = "cool" name = "fool">
  <option>Animals.json</option>
  <option>Fruits.json</option>
  <option>Birds.json</option>
  </select>
  <button id = 'first'>Search</button>
  <div></div>
  </body>
  </html>



This is supposed to work like this: when the button is clicked, the function should gather all the values associated with each "a" key in the selected json file kept in the same folder as this file, and print them into the div element. A sample json file, like Animals.json, runs like this:

[
{
"a": "Tiger" 
},
{
"a": "Elephant"
},
{
"a": "Fox"
},
{
"a": "Lion"
}
]



Can anyone spot the bug? The code is doing nothing when the button is clicked. However it prints out the selected filename if I replace the 'tex' by 'filename' in $("div").text(filename), suggesting that the bug lies in the getJSON function.

This post has been edited by cupidvogel: 03 September 2011 - 01:07 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1