JavaScript - Get JSON Data from URL

In this tutorial we will create a Get JSON Data from URL using Javascript. JavaScript is a scripting or programming language that allows you to implement complex things on web pages. It is a text-based programming language meant to run as part of a web-based application. It is an interpreted programming language that has a capabilities of Object-Oriented. So Let's do the coding...

Getting started:

This is the link for the jquery that i used in this tutorial https://jquery.com/. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6. </head>
  7. <nav class="navbar navbar-default">
  8. <div class="container-fluid">
  9. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10. </div>
  11. </nav>
  12.  
  13. <div class="col-lg-3"></div>
  14. <div class="col-lg-6 well">
  15. <h3 class="text-primary">Javascript - Get JSON Data from URL</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div></div>
  18. <div class="col-md-3"></div>
  19. <div class="col-md-6">
  20. <div class="form-group">
  21. <label>Target URL: <a target="_blank" href="https://api.flickr.com/services/feeds/photos_public">https://api.flickr.com/services/feeds/photos_public.gne</a></label>
  22. <label>Enter some Flicker Tags</label>
  23. <input type="text" id="search" class="form-control"/>
  24. </div>
  25. <center><button class="btn btn-success" id="btn_search"><span class="glyphicon glyphicon-search"></span> Search by tags</button></center>
  26. </div>
  27. <br style="clear:both;"/><br />
  28. <center><div id="result"></div></center>
  29. </div>
  30. </body>
  31. <script src="js/jquery-3.2.1.min.js"></script>
  32. <script src="js/script.js"></script>
  33. </html>

Creating the Script

This code contains the script of the application. This code will get the json data in the targetd url. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.
  1. $(document).ready(function(){
  2. $('#btn_search').on('click', function(){
  3.  
  4. var search = $('#search').val();
  5. var str = search.toLowerCase();
  6. if(search == ""){
  7. alert("Please enter something!");
  8. }else{
  9. $('#result').empty();
  10. jsonFlickrFeed = function (json) {
  11. $.each(json.items, function(i, item) {
  12. $("<div style='border:1px solid #000; float:left; width:150px; height:150px; margin:20px;'><img style='width:150px; height:150px;' src='"+item.media.m+"'/></div>").appendTo("#result");
  13. });
  14. };
  15.  
  16. $.ajax({
  17. url: 'https://api.flickr.com/services/feeds/photos_public.gne',
  18. dataType: 'jsonp',
  19. data: { "tags": str, "format": "json" }
  20. });
  21. }
  22. });
  23. });
There you have it we successfully created a Get JSON Data from URL using Javascript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Comments

Submitted byBas manga (not verified)on Sun, 10/10/2021 - 07:31

Live demo eror

Add new comment