0

Ok, so I'm new to Express. I'm messing around with sessions and ajax calls, but the problem I'm having is that whenever I run my app, my jquery doesn't work for some reason. This is the code I have:

app.js

var express    = require("express");
var mongoose   = require("mongoose");
var bodyParser = require("body-parser");
var session    = require('express-session')

var app = express();
app.use(express.static("public")); // I understand this is the directory where I would need to put all my static files: css, js, images, etc.
app.set("view engine", "jade");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

//Set secre pass for the session
app.use(session({secret:'password'}));


app.get("/",function(req, res){

    if(req.session.userName){
        res.render("admin", {session_name: req.session.userName});
    }else{
        res.render("home");
    }

});

app.post("/example-ajax", function(req, res){
    res.send(req.body.email); // return the email that was sent by the client
});

app.post("/log-in", function(req, res){
    req.session.userName = req.body.name;
    res.redirect("/");
});

app.get("/log-out", function(req, res){
    req.session.destroy();
    res.redirect("/");
});

app.listen(8080);

admin.jade

extends layout_head.jade
block content
    div(class="container")
        div(class="row")
            div(class="col-lg-6 col-lg-offset-3")
                h1 Logged In!!
                h3 Logged in as: #[b #{session_name}]
                a(href="/log-out") Log Out
                br
                div(class="btn btn-info testAjax") Test Ajax

    script(src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js")
    script(href="/css/bootstrap/js/bootstrap.min.js")
    script(href="/js/main.js")

main.js

$(document).ready(function(){
    alert("Loaded");

    $(".testAjax").on("click", function(){
        alert("test");

        $.ajax({
            type: 'POST',
            url: '/example-ajax',
            data: {
                email: "[email protected]"
            },
            success: function(data){
                // data = the email being returned from the server
                console.log("Your email is: " + data);
            }
        });
    });

});

So like I said, the jquery doesn't run whenever my page loads up or when I click the testAjax button. When I check the console, it doesn't give me any errors so I don't know what is causing the problem.

My second question is: Is this the right way to make ajax calls in Express?

Any help is greatly appreciated. Thanks.

2
  • 4
    script src and script href? looks like jquery loaded, but not other scripts Commented Aug 28, 2016 at 17:45
  • @ArtemGorlachev Haha WOW I can't believe I didn't see that! Thanks for your comment. It's working now. You should write the answer so I can accept it. Commented Aug 28, 2016 at 17:47

1 Answer 1

1

I just needed to change href to src in the script tags.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.