Getting JSON data using $.post method
Hi,
I'm learning Ajax. Right now I'm trying to catch some data from a json file and show them using $.post.
However following code is not working.
The same code works if I use
$.getJSON instead of
$.post.
I know how to bring it to work with $.ajax but I'm keen to know why it doesn't work with $.post ?
HTML:
- <main role="main">
- <button class="btn">Show Data</button>
- <div id="content"></div>
- </main>
JSON (data.josn)
- [
- {
- "name":"Barot Bellingham",
- "shortname":"Barot_Bellingham",
- },
- {
- "name":"Jonathan G. Ferrar II",
- "shortname":"Jonathan_Ferrar",
- },
- {
- "name":"Hillary Hewitt Goldwynn-Post",
- "shortname":"Hillary_Goldwynn",
- }
- ]
jQuery
- $(function() {
- $('.btn').on('click', function() {
- $.post('data.json', function(data) {
- var output = "<ul class='result'> ";
- $.each(data, function(key, val) {
- output += "<li>";
- output += val.name;
- output += "</li>";
- });
- output += "</ul>";
- $("#content").html(output);
- });
- });
- });
Thanks