Test Case for ajax fail on single result
How do I make an test case for ajax fail on single result?
Trying to identify why this code works when the json data returned has multiple items but generates an undefined error for json data with a single item.. What is a test case for ajax fail on single result? How do you make a test case for ajax fail on single result? This script and codes were developed by Martin Boyce on 20 September 2022, Tuesday.
Test Case for ajax fail on single result - Script Codes HTML Codes
<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>Test Case for ajax fail on single result</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css'>
</head>
<body> <div class="container-fluid page-wrap"> <section id="main" class="main row" role="main"> <div class="col-xs-12"> <h1>RSS to HTML via JSON</h1> <p class="lead">New Items Lists generated from RSS feeds that are fetched through Yahoo Query Language (YQL) to provide a JSON feed.</p> </div> <div id="sidebar" class="col-sm-3"> <div id="newItemsSelector" class="panel-group new-items-selector" role="tablist" aria-multiselectable="true"> <div class="panel panel-default"> <div class="panel-heading" role="tab" id="fiction"> <h3 class="panel-title"> <a role="button" data-toggle="collapse" data-parent="#newItemsSelector" href="#collapseFiction" aria-expanded="true" aria-controls="collapseFiction">Load JSON</a> </h3> </div> <div id="collapseFiction" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="fiction"> <ul class="list-group"> <li class="list-group-item"><a id="newyafiction" class="loader" href="https://webpac.sutherlandshire.nsw.gov.au/screens/newyafiction.html" data-feed="newyafiction">click to load multiple json results</a></li> <li class="list-group-item"><a id="newjuniordvds" class="loader" href="https://webpac.sutherlandshire.nsw.gov.au/screens/newjuniordvds.html" data-feed="newjuniordvds">click to load single json result</a></li> </ul> </div> </div><!-- /panel --> </div><!-- /new-items-selector --> </div><!-- /sidebar --> <div id="content" class="col-sm-9 content"> <div id="newItemsData" class="new-items-data"> <h2></h2> <p id="jsonSource"></p> <p id="subscribe" class="lead"> Source: <a id="jsonSubscribe" class="btn btn-info btn-xs" target="_blank">json</a> <a id="rssSubscribe" class="btn btn-warning btn-xs" target="_blank">RSS</a> </p> <div id='newItems' class='new-items'></div> </div> </div><!-- /content --> </section>
</div><!-- /container --> <script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script>
<script src='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js'></script> <script src="js/index.js"></script>
</body>
</html>
Test Case for ajax fail on single result - Script Codes JS Codes
$(document).ready(function() {
$('.loader').on('click', function(event) { //call the function that generates the list var $feedId = $(this).data('feed'); populateList($feedId); //prevent the default action on the link in the btn event.preventDefault();
});
var populateList = function(feedName) { var xmlSource = 'https://webpac.sutherlandshire.nsw.gov.au/feeds/' + feedName + '.xml'; var jsonSource = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20rss%20where%20url%3D'http%3A%2F%2Fwebpac.sutherlandshire.nsw.gov.au%2Ffeeds%2F" + feedName + ".xml'&format=json&callback=?"; //fetch the json feed $.getJSON(jsonSource, function() { console.log('json request:' + jsonSource); console.log('success'); }) .done(function(data) { console.log('retrieved JSON data'); $('#newItemsData > h2').text('URL to fetch json'); $('#jsonSource').text(jsonSource); $('#jsonSubscribe').attr('href', jsonSource); $('#rssSubscribe').attr('href', xmlSource); $('#newItems').empty(); //if there are items in the data generate an <li> for each if (data.query.count === 1) { var $title = data.query.results.item.title; var $url = data.query.results.item.link; var $description = data.query.results.item.description; var $isbn = data.query.results.item.guid.content.split(' ', 1); if ($description !== undefined) { $('<div class="panel panel-default"><div class="panel-heading"><h3 class="panel-title"><a target="_blank" href="' + $url + '">' + $title + '</a></h3></div><div class="panel-body">' + $description + '<br>' + $isbn + '</div></div>').appendTo("#newItems"); } else { $('<div class="panel panel-default"><div class="panel-heading"><h3 class="panel-title"><a target="_blank" href="' + $encore + '">' + $title + '</a></h3></div><div class="panel-body">' + $isbn + '</div></div>').appendTo("#newItems"); } } else if (data.query.count > 1) { $.each(data.query.results.item, function(i, book) { var $title = book.title; var $url = book.link; var $description = book.description; var $isbn = book.guid.content.split(' ', 1); if (book.description !== undefined) { $('<div class="panel panel-default"><div class="panel-heading"><h3 class="panel-title"><a target="_blank" href="' + $url + '">' + $title + '</a></h3></div><div class="panel-body">' + $description + '<br>' + $isbn + '</div></div>').appendTo("#newItems"); } else { $('<div class="panel panel-default"><div class="panel-heading"><h3 class="panel-title"><a target="_blank" href="' + $encore + '">' + $title + '</a></h3></div><div class="panel-body">' + $isbn + '</div></div>').appendTo("#newItems"); } }); } else { // if the json request is successful but there are no items console.log('no data retrieved'); } $('.new-items-data').fadeIn('slow'); }) .fail(function() { //if the fetch fails reveal the error alert .new-items-fail console.log('fail'); }) .error(function() { //if the fetch fails reveal the error alert .new-items-fail console.log('error'); });
};
});
Developer | Martin Boyce |
Username | boycetrus |
Uploaded | September 20, 2022 |
Rating | 3 |
Size | 3,297 Kb |
Views | 26,312 |
Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!
Name | Size |
Google Analytics Event Tracking using html5 data attributes | 2,560 Kb |
Google Maps API test | 4,057 Kb |
Nested flexbox layout for library catalog v2 | 3,409 Kb |
First steps with Handlebars.js | 3,978 Kb |
Google Maps API Custom Overlay | 3,336 Kb |
Fetching book covers with the Google Books API | 8,769 Kb |
Homepage thumbnail | 1,852 Kb |
Booklist Using Google Spreadsheets as JSON Source and Handlebars for templating | 3,063 Kb |
Bootstrap Panel List | 2,472 Kb |
Test Case for ajax fail on single result | 3,297 Kb |
Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!
Name | Username | Size |
Brown by pure CSS, no image, no javascript | Aaronchuo | 2,652 Kb |
Basecamp 3 Document | Lachlanjc | 3,811 Kb |
Personal Website Redesign v2.0 | DevItWithDavid | 5,168 Kb |
A Pen by Huan Nghiem | Nightshade | 10,646 Kb |
The Fly | GianlucaGuarini | 3,405 Kb |
Video Player Custom Controls | EleftheriaBatsou | 3,665 Kb |
BenU Maintenance Site | Ksherman | 4,893 Kb |
Snow collision | Wojtek1150 | 3,542 Kb |
TweenMax transformOrigin Bubble | Nicolund | 2,209 Kb |
Materializecss input form | Jasonchan | 1,443 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!