Skip to content

Instantly share code, notes, and snippets.

@prasadshir
Last active December 21, 2015 04:19
Show Gist options
  • Save prasadshir/6248353 to your computer and use it in GitHub Desktop.
Save prasadshir/6248353 to your computer and use it in GitHub Desktop.
jQuery Ajax Basic Example
[
{
"id":0,
"name":"Amitabh Bacchan",
"type":"Bollywood",
"twitter":"SrBachchan",
"desc":"Amitabh is my most favorite hero"
},
{
"id":1,
"name":"Sachin Tendulkar",
"type":"Cricket",
"twitter":"sachin_rt"
},
{
"id":2,
"name":"Deepika Padukon",
"type":"Bollywood",
"twitter":"deepikapadukone"
}
]
<html>
<head>
<script src="js/jquery-1.9.1.js"></script>
</head>
<body>
<div id="changeThis">This text will be changed by Ajax</div>
<button id="btn">Change Text</button>
<button id="btn1">Fetch Data</button>
<div id='dynamic'>
<h2>Dynamic Data</h2>
<ul></ul>
</div>
<script type="text/javascript">
$(document).ready(function(){
$("#btn").click(function(){
$("#changeThis").load("textfile.txt");
});
});
$("#btn1").click(function(){
$.ajax({
url: 'heros.json',
success: function(herosData){
console.log(herosData);
$.each(herosData, function(i,v){
$('#dynamic ul').append('<li>'+v.name+'</li>');
})
}
});
});
</script>
</body>
</html>
This is sample text from the text file!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment