Navigating to a new address using ajax and express routing

I am facing an issue with a button having an ID of 'tune-in' on a page named auth.ejs. The button is supposed to navigate to a new page called index.ejs when clicked.

However, instead of rendering the index page, clicking the button keeps me on the auth page. Nevertheless, the message "should display tune-in home page" is logged to the console.

On the HTML (ejs) page (located in the views folder > auth.ejs)

document.getElementById('tune-in').addEventListener('click', function() {
    $.ajax({
        type: 'GET',
        url: '/tune-in',
        success: function() {
            console.log("should display tune-in home page");
        }
    });
}, false);

In the controllers folder > authController.js

app.get('/tune-in', function(req, res) {
    res.render('index');
});

In the views folder > index.ejs This page contains regular HTML content and should render upon button click.

EDIT: index.js

var express = require('express');
var cookieParser = require('cookie-parser');

// The userController manages rendering the view and routing server requests
var authController = require('./controllers/authController');
var userController = require('./controllers/userController');

var app = express();

app.set('view engine', 'ejs');
app.use(express.static('./public'))
   .use(cookieParser());

authController(app);

app.listen(process.env.PORT || 4000);
console.log("Listening on port 4000...");

Answer №1

It seems like this is the solution you are looking for.

 document.getElementById('tune-in').addEventListener('click', function() {
$.ajax({
  type: 'GET',
  url: '/tune-in',
  success: function(data){
    console.log("Displaying tune-in home page");
    // Ensure data is valid html
    $("html").html(data); // Load the data
    // $("#otherid").html(data);
   }
 });
}, false);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Please proceed with submitting your choices in the order that you have selected

My goal is to submit options from a dropdown list in the order they are selected, rather than in the order they appear in the list. How can I achieve this? Here is the HTML code for the dropdown: < select style = "padding: 1em;" name = "skills" multi ...

A guide on dynamically loading images based on specified conditions in AngularJS

I am trying to display different images based on a value. If the value is greater than 3.50, image1 should be shown; if it is equal to or less than 3.50, image2 should be shown. I have attempted to write this code but I cannot find where I made a mistake. ...

Choosing the right option with the GeoPlugin technology

I am implementing the location detection feature using the plugin. I have a dropdown menu of states represented by <select>, and utilizing jQuery, I automatically select the user's detected state. $.getJSON(url, function(data){ var state = ...

Creating a Commentary System in VueJS Similar to Medium

One feature that I admire in Medium is their commenting interface, which allows users to highlight specific portions of an article and leave comments. I am interested in integrating a similar commenting feature into a VueJS application. While researching ...

Tips on integrating discretionary authentication in React/Express application?

I'm currently working on a self-hosted bookmarks manager application that utilizes React on the frontend and Express on the backend. While I have successfully implemented a standard authentication system using JWT, I am now considering making authenti ...

Correcting the reference to "/" (root) for assets after relocating the site to a subdirectory

I currently have a website located in the public_html directory, where all assets (images, css, js, etc) are referenced using /asset_folder/asset. The "/" at the beginning ensures that the browser starts from the root and navigates through the directories. ...

An error handling event for XHTML compliance, similar to the HTML onerror event, is designed to address the issue of

Below is a piece of code that handles hiding the placeholder (imagecontent) for dynamically populated empty images and their captions: <head> <script type="text/javascript"> function hideEmptyImage() { document.getElementById(&q ...

Comprehend the angular and in a confined scope and its brackets

I have been wondering why I have to use double brackets ()() when passing a method from an outer scope to a directive with an isolated scope using '&'. It seems like the function is returned with callThat() and then I need to call it with anothe ...

Finding files in WebStorm using Node.js' require statement can be done by following these

When navigating through files in WebStorm, how do I determine whether to use ../, ./, or even ..//? For instance, in the code snippet below, is it starting from the top folder? var Schema = require('./.services/.router.js'); ...

The effectiveness of recursion in asynchronous function calls within AngularJS

My task involves creating a JSON output in tree structure from recursive async calls. The code I have developed for this purpose is detailed below: $scope.processTree = function (mData, callback) { _processTree.getWebCollection( ...

What is the method for sending a file using JavaScript?

var photo = 'http://cs323230.vk.me/u172317140/d_5828c26f.jpg'; var upload_url = 'http://cs9458.vk.com/upload.php?act=do_add&mid=62..'; var xmlhttp = getXmlHttp(); var params = 'photo=' + encodeURIComponent(photo); xmlhttp ...

Is it possible for me to include a static HTML/Javascript/jQuery file in WordPress?

My extensive HTML file contains Javascript, jQuery, c3.js, style.css, and normalize.css. I am wondering if I can include this file as a static HTML page within Wordpress. Say, for instance, the file is called calculator.html, and I want it to be accessib ...

The request for an advertisement was successful, however, no ad could be displayed as there was insufficient ad inventory available. Please handle the situation appropriately with the

Using react-native, I am trying to incorporate ads into my app but encountering an error. Despite attempting various solutions, nothing seems to work. It appears that the issue may lie with the AdMob Android SDK. While I have reviewed SDK videos related to ...

New patch request received in Google Sheets, replacing the existing post request

I am transferring 12 cell values from a Google Sheet to a MongoDB database. The purpose behind this action is to merge the 12 cells, perform certain data transformations, and display the output on a frontend interface later on. Moreover, I'm faced wit ...

Angular Starter Kit

When starting with Angular.js, there are various boilerplate kits available such as angular-seed, some incorporating requirejs and more. However, many of the options I've come across seem to be outdated. As a newcomer to Angular, I'm wondering if ...

Is it possible to alter the css twice through the action of clicking on two individual buttons

One feature I have in my interface allows users to click on the edit button to bring up borders and change the background color of other divs. Once the edit button is pressed, it is replaced by cancel and save buttons. The goal is for the borders and backg ...

The functionality of Node.js's hasOwnProperty method fails to return true even for existing properties

When a user logs into my Node.js Express application using Passport, their user object is saved in the request. Here is an example of what the user object may look like: { "uuid": "caa5cb58-ef92-4de5-a419-ef1478b05dad", "first_name": ...

Bypassing the "Your connection is not private" error in NodeJS + Express with fetch() using Javascript

Presently, I have a setup with a NodeJS + ExpressJS client-side server that communicates with the back-end server via API calls. However, every time I make a call, I need to manually navigate to the URL of the API back-end server and click on Advanced -> ...

Discovering the specific DOM element that has been clicked using only JavaScript

Looking to enhance my HTML document with interactivity, I wanted to achieve a feature where clicking on a div element would display its respective id. I attempted the following approach: window.onload = function() { associateIds(); clicked(); } fu ...

The webpack-dev-server is not allowing hot reloading of HTML files

While using webpack-dev-server, I noticed that any changes made to the JavaScript file are immediately reflected on the webpage. However, when I make changes to the HTML file, the website does not update. To see the new HTML changes, I have to execute webp ...