Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.get('/courses', function (req, res) {
- var id = req.param('id'); // some variable from request
- var coursesResponse = getCourses(id); // example for getting courses
- if (!!courses && courses.length >= 0) { // if courses isn't undefined and length is greater >= 0 (example condition)
- // most important function
- res.json({
- success: true,
- courses: coursesResponse
- });
- } else {
- res.status(500).json({
- success: false,
- message: 'Internal server error while fetching courses'
- })
- }
- }
- )
- // on client side. using jquery
- // make a ajax request first and then parse the data
- var courses = $.parseJSON(data);
- // now loop through the courses array and show the values of array in a div with id 'courses'.
- $.each(courses, function(index, value) {
- $("#courses").append("<p>" + value.courseCode + " -- " + value.courseTitle + "</p>");
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement