noice23

wt 10

Jan 12th, 2026
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. 10. Develop a JavaScript program with Ajax (with HTML/CSS) for:
  2. a) Use ajax() method (without Jquery) to add the text content from the text file by sending ajax request.
  3. b) Use ajax() method (with Jquery) to add the text content from the text file by sending ajax request.
  4. c) Illustrate the use of getJSON() method in jQuery.
  5. d) Illustrate the use of parseJSON() method to display JSON values.
  6. Note: Create two separate file within the same folder one is textfile.txt and other data.json then copy
  7. below text for the both separate file and paste it save it.
  8. textfile.txt
  9. hi this is example text...
  10. data.json
  11. {"name":"John Doe","age":30,"city":"New
  12. York","skills":["JavaScript","React","Node.js"],"address":{"street":"123 Elm
  13. Street","zipcode":"10001"},"projects":[{"name":"Website
  14. Redesign","year":2023,"technologies":["HTML","CSS","JavaScript"]},{"name":"Mobile
  15. App","year":2024,"technologies":["React Native","Expo"]}]}
  16. <!DOCTYPE html>
  17. <head>
  18. <title>AJAX Examples | vtucode</title>
  19. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  20. <style>
  21. body {
  22. font-family: Arial, sans-serif;
  23. margin: 0;
  24. padding: 0;
  25. background-color: #f4f4f9;
  26. }
  27. h1 {
  28. text-align: center;
  29. color: #333;
  30. padding: 20px 0;
  31. }
  32. #content {
  33. flex-direction: column;
  34. display: flex;
  35. max-width: 600px;
  36. margin: 20px auto;
  37. padding: 20px;
  38. border: 1px solid #ddd;
  39. border-radius: 8px;
  40. background-color: #fff;
  41. box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  42. }
  43. button {
  44. display: inline-block;
  45. padding: 10px 15px;
  46. margin: 12px;
  47. border: none;
  48. border-radius: 5px;
  49. background-color: #007bff;
  50. color: #fff;
  51. font-size: 16px;
  52. cursor: pointer;
  53. transition: box-shadow 0.3s;
  54. }
  55. button:hover {
  56. box-shadow: 0 0 0 2px #fff, 0 0 0 4px #007bff;
  57. }
  58. button:focus {
  59. box-shadow: 0 0 0 2px #fff, 0 0 0 4px #007bff;
  60. }
  61. #output {
  62. display: none;
  63. margin-top: 20px;
  64. padding: 10px;
  65. border-radius: 5px;
  66. white-space: pre-wrap;
  67. max-height: 300px;
  68. overflow-y: auto;
  69. }
  70. #output.plain-ajax {
  71. background-color: #f0f8ff;
  72. border: 1px solid #b0c4de;
  73. }
  74. #output.jquery-ajax {
  75. background-color: #f5fffa;
  76. border: 1px solid #98fb98;
  77. }
  78. #output.jquery-json {
  79. background-color: #fffaf0;
  80. border: 1px solid #ffd700;
  81. }
  82. #output.parse-json {
  83. background-color: #fff0f5;
  84. border: 1px solid #ff69b4;
  85. }
  86. </style>
  87. </head>
  88. <body>
  89. <h1>AJAX Examples</h1>
  90. <div id="content">
  91. <button id="plain-ajax-btn">Load Text (Plain AJAX)</button>
  92. <button id="jquery-ajax-btn">Load Text (jQuery AJAX)</button>
  93. <button id="jquery-json-btn">Load JSON (jQuery getJSON)</button>
  94. <button id="parse-json-btn">Load and Parse JSON (jQuery get)</button>
  95. <div id="output"></div>
  96. </div>
  97. <script>
  98. function showOutput(className) {
  99. const output = document.getElementById('output');
  100. output.className = className;
  101. output.style.display = 'block';
  102. }
  103. document.getElementById('plain-ajax-btn').addEventListener('click', function () {
  104. var xhr = new XMLHttpRequest();
  105. xhr.open('GET', 'textfile.txt', true);
  106. xhr.onload = function () {
  107. if (xhr.status === 200) {
  108. document.getElementById('output').innerText = xhr.responseText;
  109. } else {
  110. document.getElementById('output').innerText = 'Error loading file.';
  111. }
  112. showOutput('plain-ajax');
  113. };
  114. xhr.send();
  115. });
  116. $('#jquery-ajax-btn').on('click', function () {
  117. $.ajax({
  118. url: 'textfile.txt',
  119. method: 'GET',
  120. success: function (data) {
  121. $('#output').text(data);
  122. },
  123. error: function () {
  124. $('#output').text('Error loading file.');
  125. }
  126. }).always(function () {
  127. showOutput('jquery-ajax');
  128. });
  129. });
  130. $('#jquery-json-btn').on('click', function () {
  131. $.getJSON('data.json')
  132. .done(function (data) {
  133. $('#output').text(JSON.stringify(data, null, 2));
  134. })
  135. .fail(function () {
  136. $('#output').text('Error loading JSON file.');
  137. })
  138. .always(function () {
  139. showOutput('jquery-json');
  140. });
  141. });
  142. $('#parse-json-btn').on('click', function () {
  143. $.get('data.json')
  144. .done(function (data) {
  145. try {
  146. let jsonData;
  147.  
  148. if (typeof data === 'string') {
  149. jsonData = JSON.parse(data);
  150. } else {
  151. jsonData = data;
  152. }
  153. $('#output').text(JSON.stringify(jsonData, null, 2));
  154. } catch (e) {
  155. $('#output').text('Error parsing JSON: ' + e.message);
  156. }
  157. })
  158. .fail(function () {
  159. $('#output').text('Error loading JSON file.');
  160. })
  161. .always(function () {
  162. showOutput('parse-json');
  163. });
  164. });
  165. </script>
  166. </body>
  167. </html>
Advertisement
Add Comment
Please, Sign In to add comment