user11508332
user11508332

Reputation: 667

Success part of Ajax post not working in fullcalendar code

In my code, the success part of an ajax post method is not working and I'm not sure why. This ajax call is part of a fullcalendar function via jquery.

The ajax request sends data to a post route, and uses that for a database cal (mysql). The passing of data and corresponding sql query works fine, but the success part of the ajax call isn't working (as evidenced by lack of console.log output)

Below is the code for the ajax call:

        $.ajax({
         url:"/insertEventsIntoDirectorInput",
         type:"POST",
         data:{title:title, start:start, end:end, urlPath: window.location.href.match(/\/([^/]*)$/)[1]},
         success:function()
         {
            console.log('insert success function started');
          calendar.fullCalendar('refetchEvents');
          alert("Added Successfully");
         }
        })

Below is the code for the post route:

router.post('/insertEventsIntoDirectorInput', function (req, res) {
    if (req.body.title) {
        var urlVariable = req.body.urlPath;
        console.log(urlVariable);
        console.log(typeof urlVariable);
        var title = req.body.title;
        var start = req.body.start;
        var end = req.body.end;

        dbjsMethods.geteIDFromDirectorInputForm(urlVariable, function(eID) {
            console.log(eID);
            console.log(typeof eID);
            dbjsMethods.insertEventsInDirectorInput(eID[0].eID,title,start,end);
        })
        console.log(res);
    }   
});

Below is the code for the sql queries (via node.js):

    geteIDFromDirectorInputForm: function (url, callback) {
        console.log('geteID function started');
        console.log(url);
        conn.query(`SELECT eID FROM directoravailabilitiesinputform where url = ?`, [url],
        function (err, results, fields) {
            if (err) throw err;
            console.log(results);
            return callback(results);
        });
    },

    insertEventsInDirectorInput: function (eID,title,start,end) {
        console.log('insert function started');
        console.log(typeof eID);
        conn.query('insert into directorinputevents(eID,title,start,end) values ("' +eID+ '","' +title+ '","' +start+ '","' + end + '")',
            function (err, results, fields) {
                if (err) throw err;
                else console.log('Updated ' + results.affectedRows + ' row(s).');
            });
    },

When I run the function, I expect there to be output of "insert success function started" as indicated by the console.log in the first bit of code, but this is absent.

Upvotes: 0

Views: 86

Answers (1)

jfriend00
jfriend00

Reputation: 708046

Your .post() handler on the server does not send a response. The client is still waiting to hear back from the server. You need to always send a response back from the server.

router.post('/insertEventsIntoDirectorInput', function (req, res) {
    if (req.body.title) {
        var urlVariable = req.body.urlPath;
        console.log(urlVariable);
        console.log(typeof urlVariable);
        var title = req.body.title;
        var start = req.body.start;
        var end = req.body.end;

        dbjsMethods.geteIDFromDirectorInputForm(urlVariable, function(eID) {
            console.log(eID);
            console.log(typeof eID);
            dbjsMethods.insertEventsInDirectorInput(eID[0].eID,title,start,end);
            res.sendStatus(200);
        })
        console.log(res);
    } else {
        res.sendStatus(500);
    }  
});

Here I inserted res.sendStatus(xxx) because it doesn't look like the client is expecting any data back from the request so this just sends the status code back with no data.


FYI, you also need to fix your geteIDFromDirectorInputForm() method to properly return errors.

geteIDFromDirectorInputForm: function (url, callback) {
    console.log('geteID function started');
    console.log(url);
    conn.query(`SELECT eID FROM directoravailabilitiesinputform where url = ?`, [url],
    function (err, results, fields) {
        if (err) {
            callback(err);
        } else {
            console.log(results);
            callback(null, results);
        }
    });
},

And, then change the calling code to look at that error:

router.post('/insertEventsIntoDirectorInput', function(req, res) {
    if (req.body.title) {
        var urlVariable = req.body.urlPath;
        console.log(urlVariable);
        console.log(typeof urlVariable);
        var title = req.body.title;
        var start = req.body.start;
        var end = req.body.end;

        dbjsMethods.geteIDFromDirectorInputForm(urlVariable, function(err, eID) {
            if (err) {
                console.log(err);
                res.sendStatus(500);
            } else {
                console.log(eID);
                console.log(typeof eID);

                dbjsMethods.insertEventsInDirectorInput(eID[0].eID, title, start, end);
                res.sendStatus(200);
            }
        });
    } else {
        res.sendStatus(500);
    }
});

Upvotes: 1

Related Questions